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!