1

Good morning,

I was trying to create a method where it retrieves a video data from a www URL and then displays it with a defined position and size by clicking a button in Unity.

However, when I start the process the video doesn't work nor do anything. After analyzing the code I noticed that the IsPrepared() method is never-ending, it gets stuck there forever. I attached my code right beneath this comment.

public IEnumerator CreateVideo(WWW www, RawImage image)
    {
        Debug.Log("Video");
        yield return www;
        //Set Size & Position
        image.GetComponent<RectTransform>().sizeDelta = new Vector2(0.5f, 0.5f);
        image.GetComponent<RectTransform>().position = new Vector3(0.5f, 0.5f, 10.0f);

        //Add VideoPlayer to the GameObject
        //videoPlayer = image.GetComponent<VideoPlayer>();

        //Add AudioSource
        // audioSource = image.GetComponent<AudioSource>();

        //Add VideoPlayer to the GameObject
        VideoPlayer videoPlayer = image.GetComponent<VideoPlayer>();
        
        //Add AudioSource
       AudioSource audioSource = image.GetComponent<AudioSource>();
        Debug.Log(audioSource);
        //Disable Play on Awake for both Video and Audio
        videoPlayer.playOnAwake = true;
        audioSource.playOnAwake = true;



        //We want to play from url
        videoPlayer.source = VideoSource.Url;
        videoPlayer.url = www.url;
       

        //Set Audio Output to AudioSource
        videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
        videoPlayer.controlledAudioTrackCount = 1;

        //Assign the Audio from Video to AudioSource to be played
        videoPlayer.EnableAudioTrack(0, true);
        videoPlayer.SetTargetAudioSource(0, audioSource);

        //Set video To Play then prepare Audio to prevent Buffering
        videoPlayer.Prepare();

        WaitForSeconds waitTime = new WaitForSeconds(5);
        while (!videoPlayer.isPrepared)
        {
            Debug.Log("Preparing Video");
            //Prepare/Wait for 5 sceonds only
            yield return waitTime;
            //Break out of the while loop after 5 seconds wait
            break;
        }

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

        //Play Video
        videoPlayer.Play();
        Debug.Log("Playing Video");

        //Play Sound
        try
        {
            audioSource.Play();

        }catch (System.Exception e)
        {
            Debug.Log(string.Format("Exception thrown whilst getting audio clip {0} ", e.Message));
        }
            
        

        
        while (videoPlayer.isPlaying)
        {
            Debug.LogWarning("Video Time: " + Mathf.FloorToInt((float)videoPlayer.time));
            yield return null;
        }

        Debug.Log("Done Playing Video");
    }

The RawImage Object is saved in the Prefab folder (in the future I want to instantiate the object), where I included several components as VideoPlayer, AudioSource, Mesh Renderer (in case it is needed) and a Transparent texture.

Any idea of the possible solution of this issue?

Thanks in advance!

HoloAir
  • 41
  • 4
  • In your code you wrote `isPrepared` and in your title you wrote `IsPrepared()`. Is that typo maybe the problem? – Geshode Jul 19 '18 at 06:23
  • Few questions: 1.How is the `CreateVideo` function called? 2.What is you Unity version? 3.Is this issue happening in the Editor or build? If build, what platform? 4.Is this issue there when you play local video instead of web video? – Programmer Jul 19 '18 at 06:47
  • @Geshode Thanks for the suggestion, but in the code I wrote isPrepared() and it seems the correct typo since I haven't receive any error in VS. – HoloAir Jul 19 '18 at 07:05
  • Then please update that in the code block in your question, because there it is still `!videoPlayer.isPrepared` as the `while` condition – Geshode Jul 19 '18 at 07:07
  • @Programmer 1. I called it by using a Coroutine, I used it also for another function (CreateImage) very similar and it worked; 2. Unity 2017.2.0f3; 3. This happens in the Editor; 4. I've just tried with a local video, but the issue is still the same... guess the WWW is not the problem – HoloAir Jul 19 '18 at 07:08
  • That's not how to play video from a url. You don't nee the `WWW` to do this. See the answer from the duplicate for the original code on how to play video from a url. See the **Play Video From URL** section. – Programmer Jul 19 '18 at 07:50
  • @Programmer I'm sorry, but I don't see the difference between what I've done and what is written in the other post. I placed a Debug.Log() beneath the www.url and it returns me the url in the format "http://[example].com" – HoloAir Jul 19 '18 at 09:39
  • There is a big difference. You don't need WWW to play video with Unity's videoplayer. Again, see the other code. Put the url directly there. Also, while waiting for, `videoPlayer.isPrepared` yield with `yield return null` instead of 5 seconds. By the way, it looks like you are waiting for 5 seconds. Is the video playing after the 5 seconds? – Programmer Jul 19 '18 at 09:53

0 Answers0