Unity Video Playback

Source: Internet
Author: User
Tags rewind

Http://www.cnblogs.com/zsb517/p/4060814.html

Background: In the game console want to join the dance elements, the first idea is to develop dance games, and then through the animation to express the dance, to provide users with dance teaching experience, but the development of the dance game itself takes a long time (too few people), do not talk about the follow-up dance and music copyright, so now want to go another way, Play Dance teaching videos in unity, add UI and some testing to achieve dance teaching, and after the framework is written, the content is easy to expand, after all, there is no need for animation for each dance design. But unity in video playback is a variety of egg pain, especially under Android, OK, the following is a lot of things from the online collation: 1 Win/macFirst, you need quick Time7.0 above on Windows and Mac. Unity supports video playback formats that are still pretty much like. mov,. mpg,. mpeg,. mp4,. avi,. asf, and even. H264 and so on, it is very simple to use, really Jane ~ ~ Single. Under Win/mac, Unity provides a movietexture texture that automatically transforms the imported video unity. Movietexture inherits with the texture class, which provides the simple control play/pause/stop required in video playback:
Pause Pauses playing the movie.
Play Starts playing the movie.
Stop Stops playing the movie, and rewinds it to the beginning.
Movietexture Given the parameters are not much, considering that the video will be downloaded from the network, detection isreadytoplay is necessary, isplaying given whether the parameters in the play, loop control loop playback, duration is a very useful thing, Gives the current location to which to play.
AudioClip Returns the audioclip belonging to the movietexture.
Duration The time, in seconds, then the movie takes to play back completely.
IsPlaying Returns whether the movie is a playing or not.
Isreadytoplay If the movie is downloading from a Web site, this returns if enough data have been downloaded so playback should being able to Start without interruptions.
Loop Set this to true to make the movie loop.
Test, the specific way to use the rain pine gives a very detailed: two ways to play the game video in Unity3d research. In the case of a planar display, the core code of the test code is as follows:
1         renderer.material.mainTexture = movie;//Set Material 2 3         Movie.loop = true;                     Loop to play 4         movie. Play ();                          Plays 5         movie. Pause ();                         Pause 6         Movie. Stop ();
The video playback feature provided in unity is still a simple one, but it doesn't matter if you just play it back, but if you're developing video, it's definitely not going to meet the requirements. For example, frequently used playback, fast forward, rewind, fixed-point playback and so on are not provided. For fast forward, there are netizens through the same gameobject binding a audiosource, using its pitch parameters can be controlled, but I did not test it out. There is also the right third-party plug-in, such as Avpro QuickTime, to enable fast forward, rewind, vertex playback, multi-window playback, and so on (if under window, the example is the video file and internal parameters.), but this plugin can only be used in win/ Use ~ ~ on Mac.      This plug-in code is a good example of a unity-based implementation, but unity has limited functionality, but it can decode the video by leveraging the video codec library, and then use the rendering capabilities (such as GL) provided by Unity's underlying to achieve the video playback capability of the surface. 2 Moving End

On the mobile side, Unity does not provide Movietexture,avpro QuickTime nor can it be used, its most basic display scheme using the Playfullscreenmovie () function for full-screen playback, according to its official website interpretation, mainly has the following aspects:

(1) The video stream is obtained directly from the device memory, so the file can only be provided separately, and the video file is placed in the Streamingassets folder.
(2) In video playback, unity will stop, unity automatically resumes after playback, and the color in the screen will change to the background color of the player and resume after playback.
(3) in iOS, the inside is actually a method of calling MPMoviePlayerController
(4) Formats to support:. mov,. mp4,. MPV, and. 3GP, h.264,mpeg-4 Part 2 video

Handheld.playfullscreenmovie ("Starwars.mp4", Color.Black, Fullscreenmoviecontrolmode.canceloninput);

Ernest, two sentences more, or decisive third-party plug-ins, Mobile movie texture for Android is a very good thing, the use of open source video codec library Theora video decoding, and then through unity texture implementation of the display. To display the effect for multiple videos:

Some questions in the application:
(1) because the video codec library uses Theora, it can only support OGG, OGV format video and audio, if you want to use other formats you need to convert, you can Theora Converter. NET tool for conversion.
(2) The video seek function is not very accurate, although it can be used
(3) Multi-video playback problem, if you want to how many video playback at the same time, need to understand the implementation of internal code, modified can be used. Another problem with multi-video playback is the efficiency problem, if it is n video, there may be n a background thread in the decoding operation, the corresponding will also have n render pipeline to render, I RK3188 on the three different video playback, there will be a lag. The key code for the middle understanding of multi-video playback is below, the Texture2d.createexternaltexture function obtains the map resource from the video, Settextures () to draw, and if multiple videos are used, multiple materials (that is, multiple render pipelines) are required.
        private void Allocatetexures () {m_channeltextures[0] = Texture2d.createexternaltexture (m_ystrid            E, M_yheight, Textureformat.bgra32, False, False, Getnativeyhandle (M_nativecontext)); M_CHANNELTEXTURES[1] = texture2d.createexternaltexture (M_uvstride, M_uvheight, Textureformat.rgba32, False, False,            Getnativecrhandle (M_nativecontext)); M_CHANNELTEXTURES[2] = texture2d.createexternaltexture (M_uvstride, M_uvheight, Textureformat.rgba32, False, False,            Getnativecbhandle (M_nativecontext));                if (m_moviematerials! = null) {for (int i = 0; i < m_moviematerials.length; ++i)                    {var mat = m_moviematerials[i];                    if (mat! = null) {Settextures (MAT);            }}}} public void Settextures (Material Material) { Material. SetTexture ("_ytex", M_channeLtextures[0]); Material.            SetTexture ("_crtex", m_channeltextures[1]); Material.            SetTexture ("_cbtex", m_channeltextures[2]); Material.            Settexturescale ("_ytex", M_uvyscale); Material.            Settextureoffset ("_ytex", M_uvyoffset); Material.            Settexturescale ("_cbtex", M_uvcrcbscale); Material.        Settextureoffset ("_cbtex", M_uvcrcboffset); }


Reprint Please specify: drizzle

Unity Video Playback

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.