2

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();
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
martinseal1987
  • 1,862
  • 8
  • 44
  • 77
  • Can you post the video url you are trying, the complete code and also mention Unity version. Does the audio work in the Editor? – Programmer Oct 01 '17 at 16:39
  • @Programmer edited the question to include your requests thanks for the reply – martinseal1987 Oct 01 '17 at 21:34
  • 1
    Before I look into this, can you just try the code from the answer in the [question](https://stackoverflow.com/questions/41144054/using-new-unity-videoplayer-and-videoclip-api-to-play-video) you linked without modiying it. Just copy and paste it then only change the `VideoSource.Url;` part to add the url. Play it and see if you have audio – Programmer Oct 01 '17 at 21:50
  • i did this and it works! why!!??? lol thanks, is it something to do with the raw image as the video player? – martinseal1987 Oct 01 '17 at 23:18
  • ive removed the raw image and this line in the answer //Assign the Texture from Video to RawImage to be displayed //image.texture = videoPlayer2.texture; and it still works in fact with the raw image i have a duplicate video.... what is this magic? lol thanks again – martinseal1987 Oct 02 '17 at 00:03
  • please add your comment as an answer and ill accept it – martinseal1987 Oct 02 '17 at 00:33

3 Answers3

2

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
0

If audio is not playing, add this line before screen.prepare():

videoPlayer.controlledAudioTrackCount = 1;
Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
m_kiani
  • 197
  • 4
  • 11
0

videoPlayer.controlledAudioTrackCount = 1; should be written when VideoSource.Url mode is used.

ruchzlive
  • 3
  • 2