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
https://video.online-convert.com/convert-to-ogv
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
Hello Nischita,
It worked fine! ๐
When i use the URL-component in the Videoplayer the videos are sometimes a little bit laggy? I guess it’s because of my internetconnection, so nervermind ๐
So I will use some Movietextures.
Thanks anyway! It was helpful!! ๐
Best regards!
Hi Jasmin, thanks for the positive feedback! Iโm glad it helped ๐
Did you try playing videos with lesser resolution? 640*360 is optimal, else Iโve noticed that it can be laggy.
Would you be able to do this with a specific twitch stream?
I looked at the video player in unity and it described the video as a clip. I am wondering if there is a max video length that can be used.
Thank you!
Hi Matthew, You’re right. The Video Player uses a clip and also has the URL capability. But the URL needs to point to a specific file (.mp4, .wav, .ogv etc) as far as I know.
For the max video length question, I have had no problems playing longer clips, It just needs to be the optimal recommended resolution of 640*360.
And for the twitch stream question, honestly I haven’t tried it. And from the ferreting that I did on the Internet on this, people don’t seem to be having any luck playing videos from YouTube or Twitch.tv for that matter ๐ If you do come across a solution, please let me also know! Thanks ๐
Hi Nischita,
I am trying to load the 3 videos, through the code (adding video player component), i have also loaded the assets from unity, the issue i am having is, the assets are not loaded up when running in emulator or on the device.
I tried with applicaiton.dataPath or persistent data path, however the assets are not loaded.
Any suggestions will be appreciated.
Thanks
Sabri
Hi Sabri, files in application dataPath and persistent dataPath will be lost each time you build. So I wouldn’t put the videos in there. Are you trying to play the videos from the Assets folder in your Unity project? Did you convert the movies from your Asset folders to MovieTexture format? Is quicktime installed to do the conversion? These would be some suggestions where you could start.. Else, screenshots and detailed description of your problem would help me figure out your exact problem more ..
Will this work for 360 videos as well?
Hi Gary, Good question! I personally haven’t tried with a 360 video. But Unity’s Video Player (which is used in this tutorial) is capable of playing 360 videos. https://unity3d.com/solutions/360video
HoloLens itself is capable of showing 360 videos – An example of this is the HoloTour app which is existing as default on the device
If you try it with a 360 video let us know too? ๐
Is There any way to play the audio source directly from the url. without having to add the audio clip
Yup. In the code, use the www audioclip which refers to an audio URL https://docs.unity3d.com/2017.4/Documentation/ScriptReference/WWW-audioClip.html
hey Nischita! Great tutorial!
I was wondering which is the easiest way to copy the script from your script pictures( for eg; Play movie script in this article). Or else I have to go ahead and start writing it myself?!
Anyways thanks for the simple tutorial! it works well.
Hey again!
Forget my previous comment. I am writing it again, it is better this way, to practice as I go. have a great day!
Hi Shrew! Exactly.. It’s better to practice by writing. if you need it, just email me (codeholo@gmail.com) and I can email it back to you. Also, thanks for the feedback ๐
Is it possible to make this pop up when i click on lets say a button?
Hi Charles, Look here for popups and other UI elements https://github.com/Microsoft/MixedRealityToolkit-Unity I’m yet to write an article using the new MRTK v2. But in case you need it urgently, this is the page to look at.
Hey Nischita! Is it possible to make this pop up when i lets say click on a button?
Hi Nischita,
Thank you for the awesome tutorial it was extremely helpful
Kind Regards
A
Hello Anthony, Welcome!
Hey Nischita,
I tried to implement the video player using the URL. It works perfectly fine when running the game on unity. But when I try to run it on my android device no video plays.
hmm android device is not my expertise.. But my first guess would be it has to do something with the shader…
Hi,
Great article!.Thanks
640 x 360 resolution help to improve the lagging issue.
Glad it helped you!
Hi Nischita,
Actually, I did solve the issue only in Unity editor but not on HL. I try HL and it is still laggy.
Any other improvement should I do?
Hi Truyen, Are you trying on the Hololens 2 or Hololens 1? Is your Internet also ok ? I had this problem when I was watching something on the Hololens and the internet was causing the slow video(youtube) and I was thinking it was something else ? Also, is your Hololens updated to latest OS?
HI Nishita,
I have HL2, and the latest os updated. This is local network which is very fast > 100Mbps.
Thanks
Truyen
Same video if I open it using Application.OpenURL() works fine with browser but not with VideoPlayer of unity.
Could you send me a screenshot of your VideoPlayer settings?
I also noticed that you are trying a very old version – try this article tutorial to play videos- https://codeholo.com/2019/04/19/take-photo-and-video-and-play-it-back-in-hololens-apps/ this could definitely help.
Thank you. I will take a look at it.
Hi Nice Blog about Unity. I don’t how to change audio of the script provided documentation of unity. I copied it exactly. Deleted Comments. I put in my Video Url. But now I can not change Volume. My clip has the audio natively loud. I want the script to lower it without no funky sliders. Just an integer setting. How do I do that? I don’t know how to implement the function they provide with track and volume. It doesn’t make sense what to do with it. Can you or another poster help?
Hey there Doan, did you have a look at this link already? https://answers.unity.com/questions/404469/decrease-volume.html The AudioSource on the AudioClip should expose a field called Volume which you can assign to a float value
Hi Nischita, nice blog!
Thanks I got a chance only to see your posted link. I think it might work, but I will report back in a week or two let you know how it went. I am juggling a full time courseload! And to think that I even tried applying for a M.A. this quarter! But yes about exposing audio source is something I want to do. Like I’m supposed to expose also the track of video clip if I could as it concerns the audio portion. I’ll get back in a week or two. Thank you!
Hi Bao, Thanks! Good luck with your course and hope the solution works ๐
Nice tutorial,,,, in my case i m trying to load a Video from json file which i upload it to Server azure. The request it works good but the problem i can hear the voice of video but without see it ..the video doesnt appear only the voice.. do you have an idea about it?
Hmm, Hard to say without looking at it fully. but do you have the right material for the Hololens video? Try the Unlit/Texture for the material for the video that you are trying to display in the Hololens.
they wrote the video Player Component will work but also many have the same problems just can hear the voice without seeing video , it doesnt even appear on unity before Hololens . Unlit/texture i have no idea about but i ll take a look ๐ i m still new in unity but thank you for your answer ,,, if i have a question later may i ask you also ?
I did it in another way. I took off audio in Premiere and rendered audio seperately…Then put all that stuff back into Unity