简体   繁体   English

Unity VideoPlayer无法播放音频

[英]Unity VideoPlayer is not playing audio

I am struggling to get the sounds out the video from Unity Video Player. 我正在努力从Unity Video Player的视频中获取声音。

The video is playing fine but the sound is not working. 视频播放正常,但声音不起作用。

Here is the code I use based on this thread. 这是我基于线程使用的代码。

    public VideoClip VideoClip;
    private AudioSource audioSource;
    private IEnumerator videoCoRoutine;

    void Awake()
    {
        VideoPlayer videoPlayer = gameObject.AddComponent<VideoPlayer>();
        audioSource = gameObject.AddComponent<AudioSource>();
        videoPlayer.clip = VideoClip;
        videoPlayer.source = VideoSource.VideoClip;
        //videoPlayer.Prepare();
        videoPlayer.renderMode = VideoRenderMode.CameraNearPlane;
        videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
        videoPlayer.EnableAudioTrack(0, true);
        videoPlayer.SetTargetAudioSource(0, audioSource);
        videoPlayer.Play();
        audioSource.Play();
        //StartCoroutine(PlayVid());
        ContinueBtn.onClick.AddListener(OnContinue);
    }

Can anyone kindly help ? 任何人都可以帮忙吗?

Thanks 谢谢

Through trial and error, here's the minimum that I've found that needs to be called to play a VideoClip from C# using Unity 2017.1 on Windows 10: 经过反复试验,这是我发现要在Windows 10上使用Unity 2017.1从C#播放VideoClip的最低要求:

public class VideoPlayerTest : MonoBehaviour {

   public VideoPlayer player;
   public VideoClip clip;
   public AudioSource audioSource;

   // Use this for initialization
   void Start () {
       player.audioOutputMode = VideoAudioOutputMode.AudioSource;
       player.SetTargetAudioSource(0, audioSource);
       player.source = VideoSource.VideoClip;
       player.clip = clip;
       player.Play();
   }
}

Using a GameObject configured like this: Unity Editor Inspector With this MP4 file (download and drop into your Assets directory): https://www.quirksmode.org/html5/videos/big_buck_bunny.mp4 使用配置如下的GameObject: Unity编辑器检查器带有以下MP4文件(下载并放入您的Assets目录): https : //www.quirksmode.org/html5/videos/big_buck_bunny.mp4

Good luck! 祝好运! I spent a lot of time fiddling with enabling tracks/channels and it didn't seem to do anything before stumbling upon this simple configuration. 我花了很多时间摆弄启用音轨/频道,在绊倒这个简单的配置之前,它似乎并没有做任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM