视频播放器协程问题

Video player coroutine issues

我有一个协程在运行时通过 www class 下载 .ogg 文件,这目前确实不一致。

该视频旨在以正常大小显示 (320 x 240px), and sometimes this works. However, frequently the video is displayed at 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();    
 }

感谢@fafase 的建议,问题似乎是"a frame gap between the downloading being ready and the video file being ready"。解决此问题的答案是检查 www.file 是否已完成下载以及视频文件是否已准备好播放,如下面的代码所示:

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...