Helpful notes on playing movies for Windows / Android

I had a hard time getting MovieTexture and Handheld.PlayFullScreenMovie working. I want to share my notes with the community to help others, because I feel the official documentation is insufficient.

On Windows, I tested .mp4 and ogg theora. .mp4 does not work, without logs or error messages to indicate failure, which was troublesome because it makes you think the problem is in your own code when it is not. Ogg theora works.

If you need a movie to test against, you can use http://unity3d.com/files/docs/sample.ogg

You cannot “new MovieTexture()” in code. If you try it will just return null. Instead, use the www class (even for files on your harddrive).
Example:
www = new WWW(“file://” + Application.streamingAssetsPath + “/sample.ogg”);
www = new WWW(“http://unity3d.com/files/docs/sample.ogg”);

You can attach MovieTexture to a game object in the editor. To access it, you use (MovieTexture) renderer.material.mainTexture. Similarly, to assign a MovieTexture for playing, set renderer.material.mainTexture = movieTexture;

When www.isDone returns true, you either have an error or the movie has finished downloading
www.progress gives a number between 0 and 1 indicating what percentage has downloaded
string.IsNullOrEmpty(www.error)==false means there was an error.

If you call movieTexture.Play(), but not enough of the movie has downloaded yet, nothing will happen. But when the movie has downloaded sufficiently, it will start playing at that time.

If you want audio, attach the AudioSource component, then assign MovieTexture.audioClip to audio.Clip, then call audio.Play()

To play a full-screen movie on Android / IOS, use Handheld.PlayFullScreenMovie(path). This code does nothing if you invoke it on Windows.
Unity does not support playing a movie on a texture on Android. The only way I had success doing this is using a modified version of Vuforia Video player. Engine Developer Portal . Get “Unity Package” under “Download Sample Code” on that page, which I missed the first time around. The folder you need is “Vuforia Video Playback” and you can delete code that references Augmented Reality.

Vuforia Video player DOES stream video from the internet despite my first impression that it did not. However, it is finicky about what it will play and silently doesn’t work if it doesn’t like the movie.

Vuforia Video player on Android just invokes Unity’s Handheld.PlayFullScreenMovie if you play fullscreen. This is confusing because a movie that works full-screen will not work on a texture, for example. This happens in the Play() function of VideoPlayerHepler.cs.

Android will not compile if it sees references to MovieTexture. Use
#if UNITY_EDITOR || UNITY_STANDALONE
// use MovieTexture
#else
// Don’t use MovieTexture
#endif

You cannot call www.movie twice. If you call it more than once, the second call will return a junk MovieTexture that won’t play a movie. For example, this works:

MovieTexture mt = www.movie;
renderer.material.mainTexture = mt;

This does not work

MovieTexture mt = www.movie;
// DO NOT DO THIS
renderer.material.mainTexture = www.movie;

Last but not least, here is some sample code that shows how to use MovieTexture in a more clear way than the official documentation. Just attach this script component to a plane, with a camera pointed at it

using UnityEngine;
using System.Collections;

public class DownloadThenPlayMovieTexture : MonoBehaviour {
	
	private WWW www;
	private bool done=false;

	void Start () {
		www = new WWW("http://unity3d.com/files/docs/sample.ogg");
	}
	
	void Update () {
		
		if (www.isDone==false  string.IsNullOrEmpty(www.error)==true)
		{
			Debug.Log("Progress: " + www.progress);
		}
		
		if (done==true)
			return;
		
		MovieTexture mt = www.movie;		
		if (www.isDone  string.IsNullOrEmpty(www.error)==false)
		{
			Debug.Log(www.error);
			done=true;
		}
		
		if (mt != null  mt.isReadyToPlay==true  mt.isPlaying == false)
		{
			audio.clip = mt.audioClip;
			audio.Play();
			
			renderer.material.mainTexture = mt;	
			mt.Play();
			done=true;
		}
	}
	
}

Won’t be needing it anytime soon, but I’m sure this will come in handy on my next few projects.
Thanks!

Hi. I’m having a hard time with the movie textures. I can watch the video when I’m on unity’s editor, but after building the project for windows standalone, the video doesn’t play when I open the app. I’m running it in the same PC, so quicktime is installed.