Play Video in Unity

Source: Internet
Author: User

Unity video Playback has a number of implementations that can be selected according to the requirements of the appropriate implementation, summarized here:
1. Movietexture
Unity standard interface, supported for playback video formats are. mov,. mpg,. mpeg,. mp4,. avi, and. asf. Only PC-side local video playback is supported.
1. Playing in a game object is like creating a plane object in the game world, with the camera directly shining on the face
In a newly created plane plane, bind its texture to a movie texture to
Sets the main texture of the current object as a movie texture
Renderer.material.mainTexture = movtexture;
Set the movie texture playback mode to loop
Movtexture.loop = true;
and can be through
Movtexture.play ();
Movtexture.pause ();
Movtexture.stop ();
for playback control.
The purpose of scaling the video can be achieved by directly scaling the plane plane.
As for the assignment of Movietexture, it is not possible to automatically create textures on the 5.0x version by dragging video into project video.


2. Play at the GUI level. It is very similar to textures, because the movietexture used to play the video belong to the sub-class of the map texture.
Draw a movie texture
Gui. Drawtexture (Newrect (0,0, screen.width,screen.height), Movtexture,scalemode.stretchtofill);
The size of the playback video is the width of the screen, if you want to dynamically modify the width or height of the video directly modify the new Rect () video display area can be
2. Handheld.playfullscreenmovie
Unity standard video playback interface with supported playback video formats are. mov,. mpg,. mpeg,. mp4,. avi, and. asf. Support pc/mobile playback, support local online play
Url_movie = "Http://dl.nbrom.cn/17051/c3e408229342723fbdf62d0bcf1d549c.mp4?fsname=Criminal_Minds_S01E01.mp4";
Handheld.playfullscreenmovie (Url_movie, Color.Black, fullscreenmoviecontrolmode.full);
Handheld.playfullscreenmovie ("Test.mp4", Color.Black, Fullscreenmoviecontrolmode.canceloninput);
Place the video file under the assets/streamingassets/path
The above method in the mobile side is the side of the download side of the network video playback, belong to the online play, the Bad Place is, again watch also need to load again. It is possible to determine if it has been downloaded locally if it is played locally, and if it is not downloaded locally from the Web
3. Easymovietexture
Unity Mobile third-party video playback plug-in, support video local playback, support RTSP.
1>. Initialize the load, which transmits the video's address (local/url) to Android in Unity, and completes the initialization of the MediaPlayer
2>. Android creates a surface and binds it to the MediaPlayer you created earlier
3>. Calculation of image stretching ratio with video drawing carrier
4>. Create videotexture based on video aspect ratio and upload to Android with
M_videotexture = new Texture2d (Call_getvideowidth (), Call_getvideoheight (), textureformat.rgb565, false);
Call_setunitytexture (M_videotexture.getnativetextureid ());
5>. Set up a video window to complete Textureid with surface
Setwindowsize (Getvideowidth (), Getvideoheight (), M_iunitytextureid, m_brockchip);
6>. Update Textures
Call_updatevideotexture ();
M_surfacetexture.updateteximage ();
7>. Play Video
Play Video using MediaPlayer
4. MediaPlayer + surfacetexture
The upper layer of the playback component is handled using MediaPlayer, and after successfully creating and setting the Setdatasource, you need to create a gl_texture_external_oes-formatted texture ID to contact MediaPlayer generation.
The reason we need to use surfacetexture here is that it can replace surfaceholder, so that when we specify the output target of the image stream as the camera preview or the video decoding, all the data we get in each frame does not need to be directly displayed on the device, Instead, you can choose to output to surfacetexture before you can do some custom extensions before the screen. When Updateteximage () is called, the content of the texture object used to create the Surfacetexture is updated to include the most recent picture in the image stream.
Surfacetexture objects can be created in any line thread. However, Updateteximage () can only be created on the line thread the OpenGL ES context containing the texture object. Callbacks that can get frame information can be called on any thread. It is important to note that if the context is not consistent, the video cannot be on the screen.
Another requirement here is that when creating textures, you need to use gl_texture_external_oes as the texture target, which is defined by the OpenGL ES extension gl_oes_egl_image_external. This texture target can impose some restrictions on how textures are used. Each time the texture is bound, it is bound to gl_texture_external_oes, not gl_texture_2d. Also, any OpenGL ES 2.0 shader that needs to be sampled from the texture needs to declare its use of this extension, for example, using the instruction "#extension Gl_oes_egl_image_external:require". These shader must also use the Samplerexternaloes sampling method to access the textures.

Play Video in Unity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.