unity 为什么音频没有在我的视频播放器上播放?

unity why is the audio not playing on my video player?

好吧,我以前从未在 unity 中使用过视频播放器,设法在 android 上获得它 运行 和编辑器,但音频无法播放,我从 url 并将视频播放器指向连接到与视频播放器组件相同的对象的音频源,我听从了这个 link 的建议,但还没有任何快乐,但下面是顺序我是怎么称呼它的,就像提到的视频一样,但音频不行,谁能帮忙

 private AudioSource audio;
 private VideoPlayer screen;

 screen = go.GetComponentInChildren<VideoPlayer>();
 audio = go.GetComponentInChildren<AudioSource>();
 audio.playOnAwake = false;
 screen.source = VideoSource.Url;
 screen.url = videoObj.VideoURL;
 screen.audioOutputMode = VideoAudioOutputMode.AudioSource;
 screen.EnableAudioTrack (0,true);
 screen.SetTargetAudioSource (0,audio);
 screen.Prepare ();
 screen.Play();
 audio.Play ();

编辑

根据下面的评论,我添加了一些信息并澄清了一些要点,因此统一版本为 2017 音频无法在编辑器或任何设备上播放,但视频在编辑器和设备上运行良好,这是一个 url 我正在尝试 https://storage.googleapis.com/godapp-a3e4b.appspot.com/jacopatrolmontage.mp4 这是完整的代码,或者我正在使用的方法请不要拆开其他东西我可能做错了因为我是新手统一和 C#

public void showPlayer(VideoObjects videoObj)
{
    if (GameObject.Find("videoPlaner(Clone)") == null)
    {
        //may need scene.isValid
        GameObject go = Instantiate(videoPlayer);
        go.transform.SetParent(canvas.transform, false);
        screen = go.GetComponentInChildren<VideoPlayer>();
        audio = go.GetComponentInChildren<AudioSource>();
        //audio = go.AddComponent<AudioSource>();
        fsscreenBool = true;
        screenSize = go.GetComponent<Transform> ().localScale;
        screenPosition = go.GetComponent<Transform> ().localPosition;
        canvasRectTransformScale = go.GetComponentInParent<RectTransform> 
        ();
        foreach (Transform tr in go.transform)
        {
            if (tr.gameObject.tag == "play")
            {
                Button play = tr.GetComponent<Button>();
                AddPlayListener(play, screen);
                preparePlayer(play,screen.isPlaying);
            }
            if (tr.gameObject.tag == "fullscreen")
            {
                Button fullScreen = tr.GetComponent<Button>();
                AddFSListener(fullScreen, go);
            }
            if (tr.gameObject.tag == "forward")
            {
            }
            if (tr.gameObject.tag == "rewind")
            {
            }
            if (tr.gameObject.tag == "vidtitle")
            {
                tr.GetComponent<Text>().text = videoObj.VideoTitle;
            }
            if (tr.gameObject.tag == "loading")
            {
                loading = tr.gameObject;
            }
        }
    }
    else{
        //print("it exists");
        GameObject existingGO = GameObject.Find("videoPlaner(Clone)");
        screen = existingGO.GetComponentInChildren<VideoPlayer>();
        loading.SetActive (true);
        foreach (Transform tr in existingGO.transform)
        {
            if (tr.gameObject.tag == "play")
            {
            }
            if (tr.gameObject.tag == "fullscreen")
            {
                checkScreen (existingGO);
            }
            if (tr.gameObject.tag == "forward")
            {
            }
            if (tr.gameObject.tag == "rewind")
            {
            }
            if (tr.gameObject.tag == "vidtitle")
            {
                tr.GetComponent<Text>().text = videoObj.VideoTitle;
            }
            if (tr.gameObject.tag == "loading")
            {
            }
        }
    }
    screen.source = VideoSource.Url;
    screen.url = videoObj.VideoURL;
    screen.audioOutputMode = VideoAudioOutputMode.AudioSource;
    screen.EnableAudioTrack (0,true);
    screen.SetTargetAudioSource (0,audio);
    screen.Prepare ();
    audio.Play ();
    screen.Play();
}

您正在调用 screen.Prepare () 而不是在调用 screen.Play()audio.Play () 之前等待它准备好。您还设置了 audio.playOnAwake = false; 但没有对视频这样做。您还应该在 playOnAwake 上将视频设为假。在播放之前给视频加载时间。

screen.Prepare();

//Wait until video is prepared
while (!screen.isPrepared)
{
    yield return null;
}

//Assign the Texture from Video to RawImage to be displayed
image.texture = videoPlayer.texture;

//Play Video
screen.Play();

//Play Sound
audio.Play();

您可能希望将音频变量重命名为其他名称,因为这是一个已弃用的变量,已声明 MonoBehaviour最后,使用您链接的问题 中的代码 。那应该可以播放 RawImage.

的视频

it still works in fact with the raw image i have a duplicate video

有一次发生在我身上,然后我意识到 Unity 在 GameObject 上寻找一个 Renderer,它在附加的任何脚本中都有任何 VideoPlayer 代码,然后自动更新 Texture那每一帧。我认为他们这样做是为了简化 VideoPlayer 的使用。

您假设将该脚本附加到一个没有 Renderer 的空游戏对象,然后您可以继续将 RawImage 放在编辑器的图像槽中。不会有重复的。

如果没有播放音频,请在 screen.prepare() 之前添加此行:

videoPlayer.controlledAudioTrackCount = 1;

videoPlayer.controlledAudioTrackCount = 1;使用VideoSource.Url模式时应写成