视频播放器协程问题

时间:2016-02-21 21:48:56

标签: video unity3d runtime unityscript coroutine

我有一个coroutine在运行时通过www类下载.ogg文件,这目前非常不一致。

视频旨在以正常尺寸(320 x 240px)显示,有时这会有效。但是,视频通常会显示在16 x 16px

在我看来,这主要是因为视频没有使用当前的协程完全下载。我尽可能地遵循文档(ScriptRef / www-movie),但无济于事,如果有人对于为什么会发生这种情况有任何建议,我将非常感激。

注意:uJS中的脚本。

非常感谢,

赖安

 function loadContextVideo(texLocation : String)
 {
     var wwwDirectory = "file://" + texLocation; 

     var vidTex = www.movie;    //method 01 [using local variables]
 //    vidTex = www.movie;    //method 02 [using public variables]

     while(!vidTex.isReadyToPlay){ //****PROBLEM COROUTINE*****
         yield www;
         if (www.isDone)
         {
             break;
         }
     }

     var texWidth = vidTex.width;
     var texHeight = vidTex.height;
     Debug.Log("texWidth: " + texWidth + " texHeight: " + texHeight);

     //check img sizes
     var mvMaxWidth = imgRect.rect.width;
     var mvMaxHeight = imgRect.rect.height;
     Debug.Log("mvMaxWidth: " + mvMaxWidth + " mvMaxHeight: " + mvMaxHeight);

     if (texHeight > mvMaxHeight)
     {
         var scaleHFactor = mvMaxHeight /texHeight;
         texHeight = texHeight * scaleHFactor;
         texWidth = texWidth * scaleHFactor;        
     }
     if (texWidth > mvMaxWidth)
     {
         var scaleWFactor = mvMaxWidth /texWidth;
         texHeight = texHeight * scaleWFactor;
         texWidth = texWidth * scaleWFactor;
     }
     Debug.Log("texWidth: " + texWidth + " texHeight: " + texHeight);
     imgRect.sizeDelta = new Vector2(texWidth, texHeight);

     //Video Tex assign
     var imgView : UI.RawImage = imageView.GetComponent.<RawImage>(); //method 01 [using local variables]
 //    var imgView = imageRender; //method 02 [using public variables]
     imgView.texture = vidTex;

     //Video Audio assign
     var vidAudio : AudioSource = imageView.GetComponent.<AudioSource>(); 
     vidAudio.clip = vidTex.audioClip;

     //curVideo = vidTex;
     vidTex.Play();
     vidAudio.Play();    
 }

1 个答案:

答案 0 :(得分:0)

感谢@ fafase的建议,问题似乎是&#34;下载准备就绪和视频文件准备就绪之间存在差距&#34; 。解决这个问题的答案是检查www.file是否已完成下载,以及视频文件是否已准备好播放,如下面的代码所示:

&#13;
&#13;
function loadContextVideo(texLocation : String)
{
	var wwwDirectory = "file://" + texLocation;
	var www : WWW = new WWW(wwwDirectory);
	
	vidTex = www.movie;
		
//	while(!vidTex.isReadyToPlay){ //problem coroutine
//		yield www;
//		if (www.isDone)
//		{
//			break;
//		}
		while(!www.isDone && !vidTex.isReadyToPlay){ //answer coroutine
		yield www;
		if (www.isDone && vidTex.isReadyToPlay)
		{
			break;
		}
	}

	var texWidth = vidTex.width;
	var texHeight = vidTex.height;

//rest of the code continues...
&#13;
&#13;
&#13;