IOS Play Video MPMoviePlayerController

Source: Internet
Author: User

Play the video at the beginning of the unity game, depending on your needs, and finally decide to use MPMoviePlayerController to achieve playback, as follows: by Tin

The size of the view needs to be modified under APPCONTROLLER.MM openeagl_unitycallback

    UIView *mainview = [[UIView alloc] Initwithframe:[[uiscreen Mainscreen] applicationframe]];     // Mainview.backgroundcolor = [Uicolor graycolor];          = MainView;            [Unitygetglviewcontroller (). View Addsubview: [Myviewcontroller Instance].view];

Need to receive unity commands in the game

//======================== Play start animation start ========================//By:xihao//2014-05-16voidPlaymovieinios (Char*path)    {[[Myviewcontroller Instance] playvideo:[nsstring Stringwithutf8string:path]]; }voidExplayvideo (Char*URL) {[[Myviewcontroller Instance] playvideo:[nsstring stringwithutf8string:url];}voidExreleasevideo () {[[Myviewcontroller Instance] releasevideo];} Movieviewcontroller*MV;-(void) PlayVideo: (NSString *) path{if(MV! =Nil)        {[MV Breakmovie];        [MV release]; MV=Nil; } MV=[[Movieviewcontroller alloc] init];    [Self.view AddSubview:mv.view]; [MV Playmovie:path];}-(void) releasevideo{if(MV! =Nil)        {[MV Breakmovie];        [MV release]; MV=Nil; } unitysendmessage ("_iosdoor","Releasevideoover","");}//======================== play start animation end ========================

Next is to play the video

MPMoviePlayerController *movie;/** @method Play Movies*/-(void) Playmovie: (NSString *) filename{Nsurl*url =[Nsurl Fileurlwithpath:filename]; //Video Playback ObjectsMovie =[[MPMoviePlayerController alloc] initwithcontenturl:url]; Movie.controlstyle=Mpmoviecontrolstylenone;    [Movie.view SetFrame:self.view.bounds]; Movie.initialplaybacktime= -1;    [Self.view AddSubview:movie.view]; //register a notification to end a play[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selec Tor (mymoviefinishedcallback:) name:mpmovieplayerplaybackdidfinishnotificat IonObject: movie]; [Movie Play];}#pragmaMark-------------------the end of the video playback delegate---------------------(void) breakmovie{if(Movie = =Nil) {        return ; }        //Destroying playback notifications[[Nsnotificationcenter Defaultcenter] removeobserver:self name: MpmovieplayerplaybackdidfinishnotificationObject: movie];    [Movie.view Removefromsuperview]; //releasing a video object[movie release]; Movie=Nil;}/*@method release objects when the video has finished playing*/-(void) Mymoviefinishedcallback: (nsnotification*) notify{NSNumber*reason =[Notify.userinfo Valueforkey:mpmovieplayerplaybackdidfinishreasonuserinfokey]; if(Reason! =Nil) {Nsinteger Reasonasinteger=[Reason IntegerValue]; Switch(Reasonasinteger) { Casempmoviefinishreasonplaybackended:{/*The movie ended normally*/                 Break; }  Casempmoviefinishreasonplaybackerror:{/*An error happened and the movie ended*/                 Break; }             Casempmoviefinishreasonuserexited:{/*The user exited the player*/                 Break; }} NSLog (@"Finish Reason =%ld", (Long) Reasonasinteger); }        /*cancels video auto-destruction performed by break MV//Video Playback object mpmovieplayercontroller* Themovie = [notify Object]; Destroy playback notifications [[Nsnotificationcenter Defaultcenter] removeobserver:self n    Ame:mpmovieplayerplaybackdidfinishnotification Object:themovie];    [Themovie.view Removefromsuperview];    Release the video object [Themovie release];    Movie = nil;     NSLog (@ "---------playvideoover 11"); */Unitysendmessage ("_iosdoor","Playvideoover",""); NSLog (@"---------Playvideoover");}

Related Article

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.