iOS comes with the use of the player

Source: Internet
Author: User

MPMoviePlayerController is introduced through Mediaplayer.frame, which can be used to play video in all formats supported by iOS, which is simple to use, but note that the implementation results are as follows: Code as follows:-(ibaction) Click: (ID) sender{//The load of the video playback view by clicking the button [self playmyvedio];} -(void) playmyvedio{//path settings, note here, do not use [Nsurl urlwithstring], but also to ensure that the path of the correct nsbundle *bundle = [NSBundle mainbundle]; NSString *moviepath = [Bundle pathforresource:@ "111/viva" oftype:@ "mp4"]; Nsurl *movieurl = [Nsurl fileurlwithpath:moviepath];//It is important that the player has been changed into a property in the header file, @property (Nonamaic,strong), If you do not write as a property will be black screen, currently do not know why player =[[mpmovieplayercontroller Alloc] Initwithcontenturl:movieurl];p layer.controlstyle= Mpmoviecontrolstyledefault; [Player Preparetoplay];  [Player.view SetFrame:self.view.bounds]; Player size [Self.view AddSubview:player.view];p layer.shouldautoplay=yes;} If you pay attention to the path of the video, and set the properties, then click on the button should be smooth to the full screen is playing video, the next issue is to exit, because it is full-screen mode, imitating the iphone's own video playback, should be clicked on the left hand corner of the done button to exit the play, if not for processing, Click After just exit fullscreen, screen or a piece of black, in fact, is the player's non-full-screen mode, so here to listen to a notification, click the Done button after the notification, so you can exit, the code is as follows:-(void) Viewdidload{[super Viewdidload]; [[Nsnotificationcenter Defaultcenter] AddobserVer:self selector: @selector (exitfullscreen:) name:mpmovieplayerdidexitfullscreennotification Object:nil];} -(void) Exitfullscreen: (nsnotification *) Notification{[player.view Removefromsuperview]; NSLog (@ "Remove player"); Update a method, the program starts to automatically play full-screen video, there is no control bar, play finished view rendering, that is, a cut-off animation, here to pay attention to the settings control bar and full-screen statements written in the Add player view, otherwise invalid setting-(void) playmyvedio{nsstring * Myfilepath = [[NSBundle mainbundle] pathforresource:@ "Mnmovnew.mp4″oftype:nil Indirectory:nil]; Nsurl *movieurl = [Nsurl Fileurlwithpath:myfilepath];p layer =[[mpmovieplayercontroller alloc] Initwithcontenturl: Movieurl]; [Player Preparetoplay]; [Self.view addsubview:player.view];//settings are written after adding player.shouldautoplay=yes; [Player Setcontrolstyle:mpmoviecontrolstylenone]; [Player Setfullscreen:yes]; [Player.view setFrame:self.view.bounds];} MPMoviePlayerController Play Video speaker no sound, plug in headphones can hear sound, depressed for a while, finally found the solution: Play Set Session type: [[Avaudiosession Sharedinstance] Setcategory:avaudiosessioncategoryplayback Error:nil];. H File Introduction header file: #import <AVFoundation/AVAudioSession.h> open Network video:-(void) Openmovie{Mpmovieplayerviewcontroller *movie = [[Mpmovieplayerviewcontroller alloc]initwithcontenturl:[nsurlurlwithstring:    @ "Video network Address"];    [Movie.movieplayer Preparetoplay];    [Self presentmovieplayerviewcontrolleranimated:movie];    [Movie.movieplayer Setcontrolstyle:mpmoviecontrolstylefullscreen];    [Movie.viewsetbackgroundcolor:[uicolorclearcolor]];    [Movie.view SetFrame:self.view.bounds]; [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (movie                                               Finishedcallback:) name:mpmovieplayerplaybackdidfinishnotification Object:movie.moviePlayer];} -(void) Moviefinishedcallback: (nsnotification*) notify{//    The video is finished or the Done button under presentmovieplayerviewcontrolleranimated is clicked to respond to the notification.    mpmovieplayercontroller* Themovie = [Notifyobject];                                                    [[Nsnotificationcenter Defaultcenter]removeobserver:selfName:mpmovieplayerplaybackdidfinishnotification Object:themovie]; [Self dismissmovieplayerviewcontrolleranimated];}    Open Local video:-(void) openmovie{nsstring *url = [[nsbundlemainbundle]pathforresource:@ "img_0322" oftype:@ "mp4"]; Mpmovieplayerviewcontroller *playerviewcontroller = [[Mpmovieplayerviewcontroller Alloc]initWithContentURL:[NSURL    Fileurlwithpath:url]];                                                [[Nsnotificationcenter Defaultcenter]addobserver:selfselector: @selector (moviefinishedcallback:) Name:mpmovieplayerplaybackdidfinishnotification object:[    Playerviewcontrollermovieplayer]];    [Self.view AddSubview:playerViewController.view];    MPMoviePlayerController *player = [Playerviewcontroller MoviePlayer]; [Player play];} -(void) Moviefinishedcallback: (nsnotification*) anotification {MPMoviePlayerController *player = [ANotification objec    T]; [[Nsnotificationcenter DEFAULTCEnter]removeobserver:self Name:MPMoviePlayerPlaybackDidFinishNotificationobject:player];    [Player stop]; [Player.view Removefromsuperview];}

iOS comes with the use of the player

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.