Unity 5.6 might be released this month. Playing Videos on Unity Apps and Games was one of the most awaited update and finally Unity is working on new video player. And here’s how to play videos on Unity using new VideoPlayer.

Introduction to Unity new VideoPlayer

VideoPlayer is a new game object component for playing back movies in your scenes in Unity. It aims at using the video hardware capabilities of both the editor and target platforms

The VideoPlayer can play movies that were imported with the new VideoClip importer. It can also read movies from StreamingAssets, local files or http sources, using progressive streaming.

[Video Tutorial] on How to play videos on Unity using new VideoPlayer

How to play videos on Unity using new VideoPlayer
Prerequisite
  • Unity 5.6 (or Unity 5.6 Beta for now, Download Here)

Download this project from Github. (Please, do not hesitate to star the project in Github, it really means alot.)

After new Unity 5.6 Beta is installed, let’s first create a new Unity 2D project, methods are almost same for Unity 3D projects as well.

After creating new Unity Project, let’s add RawImage component on which we’ll be loading out Video by adding a script to that will attach VideoPlayer to the RawImage component.

Video Formats Supported by Unity Video Player

All supported video formats:

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

Extra supported video formats on Windows:

  • avi
  • asf
  • wmf

Some of these formats don’t work on some platforms. See this post for more information on supported video formats.

Writing Script on C# to play videos on Unity using new VideoPlayer

So, to add and play video on RawImage component, let’s create a new C# script for RawImage, and name it StreamVideo. Open the C# script with any IDE, I prefer Visual Studio 2017.

Here’s what StreamVideo.cs looks like.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

public class StreamVideo : MonoBehaviour {

    public RawImage image;

    public VideoClip videoToPlay;

    private VideoPlayer videoPlayer;
    private VideoSource videoSource;

    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;
        audioSource.Pause();

        //We want to play from video clip not from url
        
        videoPlayer.source = VideoSource.VideoClip;

        // Vide clip from Url
        //videoPlayer.source = VideoSource.Url;
        //videoPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";


        //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
        WaitForSeconds waitTime = new WaitForSeconds(1);
        while (!videoPlayer.isPrepared)
        {
            Debug.Log("Preparing Video");
            //Prepare/Wait for 5 sceonds only
            yield return waitTime;
            //Break out of the while loop after 5 seconds wait
            break;
        }

        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");
    }
}

TL;DR – On the above script I’ve added a RawImage object as public so that to point to RawImage UI Component from Unity Editor. Similarly, create an object of new VideoPlayer, VideoSource and AudioSource classes. Remember this comes with the latest Unity 5.6, which is still in beta, and is supposed to be release by the end of this month.

Latest Unity bring new Classes, one of which is UnityEngine.Video that is required for VideoPlayer to work.

Playing videos on Unity from Assets folder

For this you’ll need VideoClip class and its object that holds the video clip that is to be played from the assets folder.

Create VideoClip object

public VideoClip videoToPlay;

Allocate source for videoPlayer

videoPlayer.source = VideoSource.VideoClip;
Using Video URL to play Video on Unity

To play videos for URL,  use VideoSource.Url

videoPlayer.source = VideoSource.Url;
videoPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";

Remove, the code that was written to add video clip from assets folder.

StackOverflow References: Using new Unity VideoPlayer and VideoClip API to play video by Programmer

Show 21 Comments

21 Comments

  1. Joao Silva

    Hi,

    Good job. But unfortunately i am not able to play the video on Android. The sound glitches(plays only every sec) and the image is stuck in the first frame! Anything hint you can give me? Tested Samsung Galaxy S4 Android 5.0.1

    • I am not sure if that’s the problem with your Android device, make sure the android device you are using has H.264/AAC hardware support. More on that, Unity also mentioned in their draft documentation that they were still working on Android Support. For me, it’s working on Android 7.0 on Axon 7, One Plus 3 and Honor 8. Unity released 5.6.0f1 try that version as well.

  2. Sam

    Hi.
    Very thanks, for your contribute.

    I am try use your example, but it case, I´m play video from Camera live(Streaming). I got the ip from video live, it does not work, example (http://192.168.43.192:8081).
    Your help me, to see what is my mistake. I am got the service in the raspBerry,the service name is motion.

  3. Hi,

    Great Tutorial. Unfortunately all i get is the static first frame of the video, as the video doesn’t play. Any help or advice would be greatly appreciated.

  4. sarakrael

    Hi!

    Thank you for the tutorial! But when I try to slow down or speed up the video with videoPlayer.playbackSpeed, the audio starts ticking. Any help or advice?

  5. lily

    thank you so much for shearing you knowledge with us! I followed you instruction and it really works except I could not see the video, I can hear the audio clearly but I just could not see the video, I think i had some thing wrong in my project, would you please teach me?

  6. sankar

    It not work in android. So, please help me!!!!

    • adejongh93

      hi sankar, did you get it working on android? I tested it on a Samsung Galaxy Grand Prime and doesn’t work

  7. Claz

    So can you play a movie clip AND have regular Unity gameplay with this method?

    • Yes, but make sure the “Movie Clip” is in supported formats, according to Unity’s Documentation.

  8. how to seek a video in unity????
    pls help!!!

  9. William R. Cousert

    Can I use Unity’s Video Player to play content stored on Google Drive or any other cloud storage service?

    • Hi, William. This has also been an issue for me. From what I have tried, only the URL that ends with extension of Video Files (.mp4 and other supported) can be played by Unity’s Video Player.

  10. Neer

    Thanks a lot for sharing this!

    • adejongh93

      hi, martin, did you get it working on android?

      • Div

        Stuck on the same issue. The video only shows the first frame.

  11. Sergio

    Hello,

    I saw the same steps for getting a nice video playing on Unity, however, I can’t find any answer about why all the snippets are using

    videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

    and not

    videoPlayer.audioOutputMode = VideoAudioOutputMode.Direct;

    Why do we need to add an Audio component? I tried both ways and adding an Audio component is not working an expected: sometimes the audio plays and sometimes it doesn´t. And the next time after it didn’t play, the video just don’t appear (in Unity IDE, not tested in build).

    Probably you have some hint.

    Thank you 😉

  12. Toor

    Hi, Thanks for your tutorial and article.
    I tried it but i am getting this error ” The type or namespace does not exist in the namespace ‘Unity Engine’ Are you missing assembly reference?

    Please help me out what is this issue and how to solve it?
    Thanks in Advance

Comments are closed