Search code examples
c#unity-game-engineaudiovideo-player

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


ok ive never used the video player in unity before, managed to get it running on android and the editor but the audio will not play, im getting the video from a url and pointing the video player towards an audio source attached to the same object as the video player component is attached, ive followed advice from this link Using new Unity VideoPlayer and VideoClip API to play video but havent had any joy yet below is the order of how im calling it as mentioned video works but the audio doesnt, can anyone help

 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 ();

EDIT

as per the one below comment I'm adding some info and clarifying some points, so the unity version is 2017 the audio doesnt play in the editor or on any devices but the video works fine in the editor and on devices, here is a url I'm trying https://storage.googleapis.com/godapp-a3e4b.appspot.com/jacopatrolmontage.mp4 and here is the full code, or the method I'm using please dont pull apart other stuff I'm probably doing wrong as i am rather new to unity and 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();
}

Solution

  • You are calling screen.Prepare () and not waiting for it to prepare before calling screen.Play() and audio.Play (). You also set audio.playOnAwake = false; but did not do that to the video. You should also make the video to be false on playOnAwake. Give the video time to load before playing it.

    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();
    

    You may want to rename your audio variable to something else since that is a deprecated variable that is already declared MonoBehaviour. Finally, use the code from the answer in the question you linked. That should be able to play video with a RawImage.

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

    Once happened to me then I realized that Unity looks for a Renderer on the GameObject that has any VideoPlayer code in any script attached to it then automatically update the Texture of that every frame. I think they did this to simplify the use of VideoPlayer.

    You are suppose to attach that script to an empty GameObject with no Renderer then you can go ahead and put the RawImage on the image slot in the Editor. There will be no duplicate.