【问题标题】:Video player coroutine issues视频播放器协程问题
【发布时间】:2016-02-21 21:48:55
【问题描述】:

我有一个协程在运行时通过 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();    
 }

【问题讨论】:

  • "unityscript" 现已弃用,并将从 Unity 中移除。使用 u/s 做这样的事情是非常困难的。 (幸运的是c#实际上要容易得多)
  • 嗨@Joe Blow,感谢您的建议;您在另一个 question 中向我提供了类似的反馈,虽然我认识到需要从 uJS 进行更改,但我当前项目的时间限制不允许我学习 C#。你能告诉我当前协程中的逻辑在哪里失效吗?它似乎非常接近,但并不可靠。
  • 尝试删除 www.isDone 检查。
  • 嗨@fafase,感谢您的回复!已经测试了你的建议,同样的问题似乎仍然存在。你能解释你的逻辑吗?非常感谢。

标签: video unity3d runtime unityscript coroutine


【解决方案1】:

感谢@fafase 的建议,问题似乎是“下载准备就绪和视频文件准备就绪之间的帧间隙”。解决此问题的方法是检查 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...

【讨论】:

    猜你喜欢
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    相关资源
    最近更新 更多