IMPORTANT UPDATE: Since the MovieTexture is deprecated, I have attached a VideoPlayer script which can be used instead of PlayMovie script. It is included below under the play movies dynamically part.
First of all a Happy New Year to all of you! Hope we do more awesome things with the Hololens this year 🙂
In the last post, you learnt how to import the latest MR Toolkit in your project and how to use voice interactions
This post, we will learn how to play videos in the Hololens apps using the Video Player Unity component and dynamically (via code)
So the Video Player component is so cool that we don’t even need the MR Toolkit for this one.
To get started with, create a new Unity project.
On the Main Camera set the following:
Clear Flags to Solid Color
Background to 0,0,0,0
Field of View to 80 (in this case, the movie screen will be at this optimum distance)
Clipping Planes Near to 0.85
Now we need to add a movie screen. In my experience you can project the movie onto any 3D Object. But I will chose a Plane in this example
Right click and choose Plane under 3D Object to add it. Rename it MovieScreen or something sensible
Change the values on the Main Camera so that the plane is in front of the camera and facing upright
Change the values on the MovieScreen to also have optimal viewing conditions. I reduced the scale to 0.3 on all Axes so that the moviescreen appears of the right size
Now we will add a video. Let’s try with a sample .mp4 file. I downloaded it from http://www.sample-videos.com/index.php#sample-mp4-video which offers samples for free. Move this to your Assets folder- it’s easier that way
Tip: If you need to convert your own videos to different file formats, I found this site very useful
On the MovieScreen’s Add Component, Add Video Player
On the Video Player make the following changes:
Choose Source to Video Clip
Drag and drop your video to the Video Clip part
Choose Play on Awake because you want the video to play as soon as the app starts
I also checked on Loop because I want the video to continuously play
The Video Player will need an audio source. Attach an Audio Source to the Movie Screen via Add Component
Drag and drop the Audio Source into the Audio Source of the Video Player
That’s it! Save the scene and build the app. Test it on the Hololens or the emulator. You should be able to see it play!
The formats I have tried with are .flv, .mp4, .ogv and .wav. They all work.
To use the Video Player with URL,
Simply change the Source to URL and type in the path where your file is stored. It needs to be an absolute path so don’t add a link to Youtube or so
Try this link for example: http://techslides.com/demos/sample-videos/small.mp4
We need to do a slight change here with the Texture and the material. In the previous case, since we gave the Video Player the file directly, the texture and material was implicitly converted to play in the Video Player. But we are now feeding the Video Player a URL of the video. Therefore, you need to provide it a texture and material manually of the video.
In the Assets folder of the Unity project, right click and Create new Render Texture. Rename it MyTexture or something sensible
Change the Render Mode to Render Texture
Drag and drop MyTexture to Target Texture
Drag MyTexture to your MovieScreen. This will add a new folder called Materials to your Assets folder. You’ll also see that the Materials on the Movie Screen will be changed to MyTexture
Note that you have to do this only once. And you can change the URLs to wherever your files are present and the Render Texture that you created will take care of conversion to the proper texture to display it on the screen
In your Video Player, make sure you make these changes
The final step: Make sure that in Edit -> Project Settings -> Player -> Publishing Settings, the Internet Client is selected under Capabilities. This will ensure that the app will connect to the Internet (of course your Hololens should be connected to the Internet as well) and pick up the video from the URL
Build and try it. The video should play
Now for the part where you play the videos dynamically
Create a New Scene in the same project (or a new one)
Create a MovieScreen (as explained before). Don’t forget to change the values of the Plane and the Main Camera for optimal viewing
On the MovieScreen, Add component-> add a new script and call it VideoPlayerScript.cs or something relevant. (this part is a recent change in the article from the previously used MovieTexture which is deprecated to VideoPlayer)
Edit the script and add the following code
Since the videoClip variable is public, you will see it as a Inspector value in the project. This is where we need to add the VideoClip of the video we choose to play. The best part here is you don’t need to convert anything. The videos in Unity are by default available as VideoClip format. If not, click on the Video and Import it as Video Clip in it’s Inspector.
(Playing the video via The MovieTexture part has been deprecated. However it is below for reference)
On the MovieScreen, Add Component – > add a new script and call it PlayMovie or something relevant.
Edit the script and add the above code
Since the movieToPlay variable is public, you will see it as a Inspector value in the project. This is where we need to add the MovieTexture of the video we choose to play
But first we need to convert the Movie in our Assets folder (save a movie in your asset folder) to a MovieTexture
Click on the video file and choose MovieTexture in Importer Version. Then click on Apply.
This will convert the video in the Assets folder to a Video Texture format
Now drag and drop the VideoTexture that you just converted to the Movie To Play field of PlayMovie
The last step is to add an Audio Source to the MovieScreen via Add Component
Build and play!
Errors and Solutions
Error: If your video is playing in breaks and interrupted frequently
Solution: Reduce the pixels of the video to 640 x 360 and it should play fine on the Hololens
Error: The video is blank
Solution: The texture and material is not converted properly. Follow the instructions above to convert the texture and feed the Video Player the texture