Free VideoPlayer

Hi,

I want to share with you guys some code to improve the Video playback on Unity.
I only tested it on a PC, but I guess this code could help some one:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;

public class DynamicVideo : MonoBehaviour {

    public string _url = "http://www.unity3d.com/webplayers/Movie/sample.ogg";
   
    public RawImage texture;
    private MovieTexture movieTexture;
    private WWW www;

    public bool autoPlay = true;
    private bool autoPlayed = false;
   
    public bool loop = true;

    //events
    UnityEvent VideoStart = new UnityEvent ();
    UnityEvent VideoEnd = new UnityEvent ();

    //fades
    private float alpha = 1f;
    public bool fadeIn = true;
    public bool fadeOut = true;
    public float fadeInDuration = 2.0f;
    public float fadeOutDuration = 2.0f;
    private bool isFadingIn;
    private bool isFadingOut;
    private float alphaStart;
    private float fadeCounter = 0.0f;

    //timer
    public Text TimeCodeCurrent;
    public Text TimeCodeDuration;
    private float currentTime = 0;

    //video complete verification
    private bool isPlaying = false;
    private int frameFreezedMax = 3;
    private int frameFreezedCounter = 0;
    private float lastCurrentTime = 0;

    void Start () {
        Url = _url;
    }

    void Update () {

        //autoPlay
        if (autoPlay && !autoPlayed && !movieTexture.isPlaying && movieTexture.isReadyToPlay) {
            autoPlayed = true;
            Play ();
        }
       
        //update alpha
        if((isFadingIn || isFadingOut) && movieTexture.isReadyToPlay){
            fadeCounter += Time.deltaTime;

            if (isFadingIn) {
                if (alpha < 1f) {
                    alpha = Mathf.Lerp(alphaStart, 1, fadeCounter/fadeInDuration);
                } else {
                    //fadeIn complete
                    alpha = 1.0f;
                    isFadingIn = false;
                }
            }

            if (isFadingOut) {
                if(alpha>0f) {
                    alpha = Mathf.Lerp(alphaStart, 0, fadeCounter/fadeOutDuration);
                }else{
                    //fadeOut complete
                    alpha = 0.0f;
                    isFadingOut = false;
                }
            }
        }
        texture.color = new Color (texture.color.r, texture.color.g, texture.color.b, alpha);

        //update texture and timer
        if (movieTexture.isPlaying) {
            texture.texture = movieTexture;
            currentTime += Time.deltaTime;
        }

        //verify video complete
        if (isPlaying) {
            //it checks if the movie has stop update, and after some frames it considers it is completed
            frameFreezedCounter = currentTime == lastCurrentTime ? frameFreezedCounter+1 : 0;
            if(frameFreezedCounter == frameFreezedMax) OnVideoEnd();
            lastCurrentTime = currentTime;
        }

        //update timecode
        if(TimeCodeCurrent) TimeCodeCurrent.text = string.Format("{0:mm:ss; mm:ss}", System.TimeSpan.FromMinutes(currentTime));
        if(TimeCodeDuration) TimeCodeDuration.text = string.Format("{0:mm:ss; mm:ss}", System.TimeSpan.FromMinutes(movieTexture.duration));
    }

    private void OnVideoStart(){
        VideoStart.Invoke ();
    }

    private void OnVideoEnd(){
        VideoEnd.Invoke ();
        if (loop) {
            Stop (false);
            Play (false);
        } else {
            Stop ();
        }
    }
   
    public string Url
    {
        get { return _url; }
        set { 
            _url = value;
            www = new WWW(_url);
           
            movieTexture = www.movie;
            if(audio) audio.clip = movieTexture.audioClip;
           
            autoPlayed = false;
           
            if(fadeIn) alpha = 0.0f;
        }
    }
   
    public float CurrentTime
    {
        get { return currentTime; }
    }
   
    public float Duration
    {
        get { return movieTexture.duration; }
    }
   
    public void Play(bool canFadeIn = true) {
        isPlaying = true;
        movieTexture.Play ();
        if(audio) audio.Play ();
        if(currentTime==0) OnVideoStart();
        if(canFadeIn && fadeIn && alpha<1.0f) {
            fadeCounter = 0;
            alphaStart = alpha;
            isFadingIn = true;
            isFadingOut = false;
        }
    }
   
    public void Stop(bool canFadeOut = true) {
        isPlaying = false;
        movieTexture.Stop ();
        if(audio) audio.Stop ();
        currentTime = 0;
        lastCurrentTime = 0;
        if (canFadeOut && fadeOut) {
            fadeCounter = 0;
            alphaStart = alpha;
            isFadingOut = true;
            isFadingIn = false;
        }
    }
   
    public void Pause() {
        isPlaying = false;
        movieTexture.Pause ();
        if(audio) audio.Pause ();
    }
   
    public void TooglePlayPause() {
        if (!movieTexture.isReadyToPlay) return;
        if (movieTexture.isPlaying) {
            Pause();
        } else {
            Play();
        }
    }
}

PS1: I am tired to search for unity code and realize that every coder put basic stuffs to sell here, this is sad…
PS2: I am also sad with the video playback on unity, where is the seek and current time options?

1 Like

Hi, how to use this ?

Be aware: movieTexture is Pro only! It won’t work in Unity Free.