早上好
我试图创建一种方法,该方法可从www URL检索视频数据,然后通过单击Unity中的按钮以定义的位置和大小显示它。
但是,当我开始该过程时,视频不起作用也不做任何事情。在分析了代码之后,我注意到IsPrepared()方法永无止境,它永远卡在那里。我将代码附加在此注释的下面。
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");
}
RawImage对象保存在Prefab文件夹中(将来我想实例化该对象),其中包括几个组件,如VideoPlayer,AudioSource,Mesh Renderer(如果需要)和透明纹理。
对这个问题的可能解决方案有任何想法吗?
谢谢!