Issues with video player coroutine
Hi Unity Community
I have a coroutine downloading an .ogg file at runtime via the www class, this is currently really inconsistent...
The video is intended to display at its normal size (320 x 240px), and sometimes this works.
However, a lot of the time the video is displayed at 16 x 16px.
It seems to me that this is largely due to the video not being downloaded completely using the current coroutine. I have followed the docs to the best of my ability but to no avail, if anyone has any suggestions as to why this may be occurring I would be very grateful.
NB: scripting in uJS.
Many thanks in advance, Ryan
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();
}
Answer by RyanAchtenSoma · Feb 25, 2016 at 08:30 AM
The issue seems to be a frame gap between the downloading being ready and the video file being ready. The answer to resolving this is to check both whether the www.file is done downloading and whether the video file is ready to be played as in the code below:
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...
Answer by Pratap-Dafedar · Feb 25, 2016 at 08:30 AM
This is usual thing, which even happens in youtube as well. If internet is slow, then youtube will automatically plays lower resolution format of video. and changes to high when speed is good. because of auto adjustment.
Even the doc says the samething.
Even if the movie is not yet completely downloaded, this returns immediately, allowing you to start playing the partial movie as it downloads.
So you shouldn't rely on the size(Rect) of the movie downloaded. Make a fixed width height for video player. and play the downloaded video. And then it should play well.