Unievrsity of Games University of Games

Using new Unity Engine VideoPlayer and VideoClip API to play video

MovieTexture is finally deprecated after Unity 5.6.0b1 release and new API that plays video on both Desktop and Mobile devices is now released.

Using new Unity Engine VideoPlayer and VideoClip API to play video

Finally Unity team decided to deprecate MovieTexture after Unity 5.6.0b1 — link, release and shows new API called VideoClip and VideoPlayer which manage, control and plays video on both Desktop and Mobile devices. Thanks to the new solution we can easly play video or retrieve texture for each frame.

How to use VideoClip and VideoPlayer?

Play Video From URL:

//We want to play from video clip not from url  
videoPlayer.source = VideoSource.VideoClip;  
with://We want to play from url  
videoPlayer.source = VideoSource.Url;  
videoPlayer.url = "YOUR\_VIDEO.mp4";

Play Video From StreamingAssets folder:

string url = "file://" + Application.streamingAssetsPath + "/" + "YOUR\_VIDEO.mp4";if !UNITY\_EDITOR && UNITY\_ANDROID  
    url = Application.streamingAssetsPath + "/" + "VideoName.mp4";  
#endif//We want to play from url  
videoPlayer.source = VideoSource.Url;  
videoPlayer.url = url;

All supported video formats:

  • ogv
  • vp8
  • webm
  • mov
  • dv
  • mp4
  • m4v
  • mpg
  • mpeg

Extra supported video formats on Windows:

  • avi
  • asf
  • wmf

Remember that, some of these formats don’t work on some platforms. See post on Unity Forum: https://forum.unity3d.com/threads/correct-video-format-for-video-playback.446011/#post-2886983 for more information on supported video formats.

Why Audio was not playing:

We have managed to get video working, however as you have notice our demo couldn’t get the audio to play.

//Set Audio Output to AudioSource  
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;//Assign the Audio from Video to AudioSource to be played  
videoPlayer.EnableAudioTrack(0, true);  
videoPlayer.SetTargetAudioSource(0, audioSource);  
must be called before videoPlayer.Prepare(); not after it. This is took hours of experiment to find this this was the problem I was having.

This should work but you may experience buffering when the video starts playing. While using this temporary fix, my suggestion is to file for bug with the title of “videoPlayer.isPrepared always true” because this is a bug.

Some people also fixed it by changing:

videoPlayer.playOnAwake = false;   
audioSource.playOnAwake = false;

to

videoPlayer.playOnAwake = true;   
audioSource.playOnAwake = true;

Here is our final script

//Raw Image to Show Video Images \[Assign from the Editor\]  
public RawImage image;  
//Video To Play \[Assign from the Editor\]  
public VideoClip videoToPlay;private VideoPlayer videoPlayer;  
private VideoSource videoSource;//Audio  
private AudioSource audioSource;// Use this for initialization  
void Start()  
{  
    Application.runInBackground = true;  
    StartCoroutine(playVideo());  
}IEnumerator playVideo()  
{  
    //Add VideoPlayer to the GameObject  
    videoPlayer = gameObject.AddComponent<VideoPlayer>();//Add AudioSource  
    audioSource = gameObject.AddComponent<AudioSource>();//Disable Play on Awake for both Video and Audio  
    videoPlayer.playOnAwake = false;  
    audioSource.playOnAwake = false;//We want to play from video clip not from url  
    videoPlayer.source = VideoSource.VideoClip;//Set Audio Output to AudioSource  
    videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;//Assign the Audio from Video to AudioSource to be played  
    videoPlayer.EnableAudioTrack(0, true);  
    videoPlayer.SetTargetAudioSource(0, audioSource);//Set video To Play then prepare Audio to prevent Buffering  
    videoPlayer.clip = videoToPlay;  
    videoPlayer.Prepare();//Wait until video is prepared  
    while (!videoPlayer.isPrepared)  
    {  
        Debug.Log("Preparing Video");  
        yield return null;  
    }Debug.Log("Done Preparing Video");//Assign the Texture from Video to RawImage to be displayed  
    image.texture = videoPlayer.texture;//Play Video  
    videoPlayer.Play();//Play Sound  
    audioSource.Play();Debug.Log("Playing Video");  
    while (videoPlayer.isPlaying)  
    {  
        Debug.LogWarning("Video Time: " + Mathf.FloorToInt((float)videoPlayer.time));  
        yield return null;  
    }Debug.Log("Done Playing Video");  
}

To help us create more free stuff, consider leaving a donation through Patreon where you can vote for the next asset pack and get access to future packs & articles earlier. Sharing knowledge is our passion. Unfortunately, keeping a team of professionals costs a lot that’s why we are constantly taking on new opportunities.

​We’ll be publishing any news on the following outlets:

Thanks for reading!

Leszek W. Król

Leszek W. Król

On a daily basis, I accompany companies and institutions in designing strategies and developing new products and services.