I am trying to return the length of the movie. I have tried everything from using web based videos and local videos. I am calling
movie.duration
after I play the video, I have even checked it while it’s playing and it only returns -1. I haved tried .ogg, .ogv, .mpg4 and .mp3 files. I have also tried to just get the audio length but still it just says -1. Is movie.duration broken in Unity 3x? I haven’t been able to find a soluation yet. Any ideas?
Here is my code,
private IEnumerator _PlayMovie()
{
if(currentMovie != null)
currentMovie.Stop();
timer = 0;
var www = new WWW(url);
var movie = www.movie;
while (!movie.isReadyToPlay)
{
yield return new WaitForEndOfFrame();
}
currentMovie = movie;
myGUITexture.texture = currentMovie;
audio.clip = currentMovie.audioClip;
movie.Play();
audio.Play();
movieTime = movie.duration;
}
void Update()
{
if (currentMovie.isPlaying)
{
if(!audio.isPlaying)
audio.Play();
Debug.Log("Movie: " + currentMovie.duration);
Debug.Log("AC: " + audio.clip.length);
}
}
If you convert your movies to AVI and let Unity internally convert them to ogv, it seems the duration works. This is what I did to fix the problem. Seems this is a bug that the Unity people could fix?
This seems to still be the case in the 4.6 public beta 21. Using an OGV from other tools does not know its length - but letting Unity3D convert some other formats in the editor does end up with a valid duration (but has other problems). Workaround is to add video length as meta-data elsewhere, but this is prone to error.