Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Free VideoPlayer

Discussion in 'Assets and Asset Store' started by VamossVoar, Oct 24, 2014.

  1. VamossVoar

    VamossVoar

    Joined:
    Oct 24, 2014
    Posts:
    4
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Events;
    4. using System.Collections;
    5.  
    6. public class DynamicVideo : MonoBehaviour {
    7.  
    8.     public string _url = "http://www.unity3d.com/webplayers/Movie/sample.ogg";
    9.    
    10.     public RawImage texture;
    11.     private MovieTexture movieTexture;
    12.     private WWW www;
    13.  
    14.     public bool autoPlay = true;
    15.     private bool autoPlayed = false;
    16.    
    17.     public bool loop = true;
    18.  
    19.     //events
    20.     UnityEvent VideoStart = new UnityEvent ();
    21.     UnityEvent VideoEnd = new UnityEvent ();
    22.  
    23.     //fades
    24.     private float alpha = 1f;
    25.     public bool fadeIn = true;
    26.     public bool fadeOut = true;
    27.     public float fadeInDuration = 2.0f;
    28.     public float fadeOutDuration = 2.0f;
    29.     private bool isFadingIn;
    30.     private bool isFadingOut;
    31.     private float alphaStart;
    32.     private float fadeCounter = 0.0f;
    33.  
    34.     //timer
    35.     public Text TimeCodeCurrent;
    36.     public Text TimeCodeDuration;
    37.     private float currentTime = 0;
    38.  
    39.     //video complete verification
    40.     private bool isPlaying = false;
    41.     private int frameFreezedMax = 3;
    42.     private int frameFreezedCounter = 0;
    43.     private float lastCurrentTime = 0;
    44.  
    45.     void Start () {
    46.         Url = _url;
    47.     }
    48.  
    49.     void Update () {
    50.  
    51.         //autoPlay
    52.         if (autoPlay && !autoPlayed && !movieTexture.isPlaying && movieTexture.isReadyToPlay) {
    53.             autoPlayed = true;
    54.             Play ();
    55.         }
    56.        
    57.         //update alpha
    58.         if((isFadingIn || isFadingOut) && movieTexture.isReadyToPlay){
    59.             fadeCounter += Time.deltaTime;
    60.  
    61.             if (isFadingIn) {
    62.                 if (alpha < 1f) {
    63.                     alpha = Mathf.Lerp(alphaStart, 1, fadeCounter/fadeInDuration);
    64.                 } else {
    65.                     //fadeIn complete
    66.                     alpha = 1.0f;
    67.                     isFadingIn = false;
    68.                 }
    69.             }
    70.  
    71.             if (isFadingOut) {
    72.                 if(alpha>0f) {
    73.                     alpha = Mathf.Lerp(alphaStart, 0, fadeCounter/fadeOutDuration);
    74.                 }else{
    75.                     //fadeOut complete
    76.                     alpha = 0.0f;
    77.                     isFadingOut = false;
    78.                 }
    79.             }
    80.         }
    81.         texture.color = new Color (texture.color.r, texture.color.g, texture.color.b, alpha);
    82.  
    83.         //update texture and timer
    84.         if (movieTexture.isPlaying) {
    85.             texture.texture = movieTexture;
    86.             currentTime += Time.deltaTime;
    87.         }
    88.  
    89.         //verify video complete
    90.         if (isPlaying) {
    91.             //it checks if the movie has stop update, and after some frames it considers it is completed
    92.             frameFreezedCounter = currentTime == lastCurrentTime ? frameFreezedCounter+1 : 0;
    93.             if(frameFreezedCounter == frameFreezedMax) OnVideoEnd();
    94.             lastCurrentTime = currentTime;
    95.         }
    96.  
    97.         //update timecode
    98.         if(TimeCodeCurrent) TimeCodeCurrent.text = string.Format("{0:mm:ss; mm:ss}", System.TimeSpan.FromMinutes(currentTime));
    99.         if(TimeCodeDuration) TimeCodeDuration.text = string.Format("{0:mm:ss; mm:ss}", System.TimeSpan.FromMinutes(movieTexture.duration));
    100.     }
    101.  
    102.     private void OnVideoStart(){
    103.         VideoStart.Invoke ();
    104.     }
    105.  
    106.     private void OnVideoEnd(){
    107.         VideoEnd.Invoke ();
    108.         if (loop) {
    109.             Stop (false);
    110.             Play (false);
    111.         } else {
    112.             Stop ();
    113.         }
    114.     }
    115.    
    116.     public string Url
    117.     {
    118.         get { return _url; }
    119.         set {
    120.             _url = value;
    121.             www = new WWW(_url);
    122.            
    123.             movieTexture = www.movie;
    124.             if(audio) audio.clip = movieTexture.audioClip;
    125.            
    126.             autoPlayed = false;
    127.            
    128.             if(fadeIn) alpha = 0.0f;
    129.         }
    130.     }
    131.    
    132.     public float CurrentTime
    133.     {
    134.         get { return currentTime; }
    135.     }
    136.    
    137.     public float Duration
    138.     {
    139.         get { return movieTexture.duration; }
    140.     }
    141.    
    142.     public void Play(bool canFadeIn = true) {
    143.         isPlaying = true;
    144.         movieTexture.Play ();
    145.         if(audio) audio.Play ();
    146.         if(currentTime==0) OnVideoStart();
    147.         if(canFadeIn && fadeIn && alpha<1.0f) {
    148.             fadeCounter = 0;
    149.             alphaStart = alpha;
    150.             isFadingIn = true;
    151.             isFadingOut = false;
    152.         }
    153.     }
    154.    
    155.     public void Stop(bool canFadeOut = true) {
    156.         isPlaying = false;
    157.         movieTexture.Stop ();
    158.         if(audio) audio.Stop ();
    159.         currentTime = 0;
    160.         lastCurrentTime = 0;
    161.         if (canFadeOut && fadeOut) {
    162.             fadeCounter = 0;
    163.             alphaStart = alpha;
    164.             isFadingOut = true;
    165.             isFadingIn = false;
    166.         }
    167.     }
    168.    
    169.     public void Pause() {
    170.         isPlaying = false;
    171.         movieTexture.Pause ();
    172.         if(audio) audio.Pause ();
    173.     }
    174.    
    175.     public void TooglePlayPause() {
    176.         if (!movieTexture.isReadyToPlay) return;
    177.         if (movieTexture.isPlaying) {
    178.             Pause();
    179.         } else {
    180.             Play();
    181.         }
    182.     }
    183. }
    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?
     
    mgear likes this.
  2. SNSD

    SNSD

    Joined:
    Jun 17, 2014
    Posts:
    5
    Hi, how to use this ?
     
  3. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,663
    Be aware: movieTexture is Pro only! It won't work in Unity Free.