Sign up with your email address to be the first to know about new products, VIP offers, blog features & more.

 




How to play videos on Unity using new VideoPlayer

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

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, lets 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.

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

Allocate source for videoPlayer

Using Video URL to play Video on Unity

To play videos for URL,  use VideoSource.Url

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

 

Thank you for your love.
share
21 Responses
  • Joao Silva
    March 27, 2017

    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

    • Code Team
      March 28, 2017

      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.

  • Sam
    April 28, 2017

    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.

  • Adam
    May 1, 2017

    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.

  • sarakrael
    May 11, 2017

    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?

  • lily
    June 3, 2017

    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?

  • sankar
    July 27, 2017

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

    • Code Team
      August 1, 2017

      Please make sure the Video Format is supported.

    • adejongh93
      September 27, 2017

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

  • Claz
    August 3, 2017

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

    • Code Team
      August 4, 2017

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

  • sankar
    August 14, 2017

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

  • William R. Cousert
    September 19, 2017

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

    • Code Team
      September 20, 2017

      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.

  • Neer
    September 25, 2017

    Thanks a lot for sharing this!

  • martin
    September 25, 2017

    ive been experimenting with the video player it runs fine in the engine but will not play on android found this https://docs.google.com/document/d/1gZa5z_jEEETDk8E_hbYXXjNhetRlM9eu8eTpbfMkRYY/edit

    • adejongh93
      September 27, 2017

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

      • Div
        October 3, 2017

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

  • Sergio
    November 14, 2017

    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

    and not

    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 😉

  • Toor
    November 18, 2017

    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

  • […] that are starting to become more popular include multimedia databases, which store pictures, video clips, and sounds, Geographic Information Systems (GIS) that store maps, satellite images and weather […]

What do you think?

Your email address will not be published. Required fields are marked *

%d bloggers like this: