Streaming Movie From A File In Unity

Code

using System;
using UnityEngine;
using System.Collections;
 
[RequireComponent(typeof(AudioSource))]
public class ATest : MonoBehaviour
{
    public MovieTexture movieTexture;
    protected bool streamReady = false;
 
    void Start ()
    {        
        StartCoroutine(StartStream(@"file:///Users/ray.pendergraph/test.ogv"));
    }
 
    protected IEnumerator StartStream (String url)
    {
        WWW videoStreamer = new WWW (url);
        movieTexture = videoStreamer.movie;
        audio.clip = movieTexture.audioClip;
 
        while (!movieTexture.isReadyToPlay) {
            yield return 0;
        }
 
        audio.Play ();
        movieTexture.Play ();
        streamReady = true;
    }
 
    void OnGUI ()
    {
        if (streamReady) {
            GUI.DrawTexture (new Rect (0, 0, Screen.height, Screen.width), movieTexture);    
        }
    }
}

Commentary

This is a simple proof of concept for streaming a ogv file from the disk. Loading an ogv from an asset bundle is painful because the bundle takes so long to load if you have a few movies that are large. In my case I have several that total over 100MB.

Snippet Usage

Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.