Mpmovieplayer Implementation video Audio player in iOS Author: Han Junqiang

Source: Internet
Author: User
Tags notification center

iOS plays video files generally using Mpmovieplayerviewcontroller and MPMoviePlayerController. The former is a view and the latter is a controller. The difference is that Mpmovieplayerviewcontroller contains a MPMoviePlayerController

Note: Mpmovieplayerviewcontroller must be added presentmovieplayerviewcontrolleranimated way, Otherwise the Done button is not responsive to notify the Mpmovieplayerplaybackdidfinishnotification event;

First, include #import <MediaPlayer/MediaPlayer.h> header files and mediaplayer.framework.

Mpmoviecontrolmodedefault Show Play/Pause, volume and time control

Mpmoviecontrolmodevolumeonly only show Volume control

Mpmoviecontrolmodehidden No Controller

You can use the following width-to-height ratios:

Mpmoviescallingmodenone do not do any scaling

Mpmoviescallingmodeaspectfit adapt to screen size, maintain aspect ratio

Mpmoviescallingmodeaspectfill adapts to screen size, maintains aspect ratio, can be cropped

Mpmoviescallingmodefill full screen, do not maintain aspect ratio

Notice

Mpmovieplayercontentpreloaddidfinishnotification is emitted after the movie player finishes preloading the content. Because the content can be played only as part of the load, this notification may not be issued until it has been played.

Mpmovieplayerscallingmodedidchangednotification is emitted when the user changes the zoom mode of the movie. Users can tap the zoom icon to toggle between full screen and window playback.

Mpmovieplayerplaybackdidfinishnotification when the movie is finished or the user presses the Done button

===============================================================================

Framework to be introduced: mediaplayer.framework

First step: Introduce Framework settings properties

#import "RootViewController.h" #import <MediaPlayer/MediaPlayer.h> @interface Rootviewcontroller () @property ( Nonatomic, strong) MPMoviePlayerController *movieplayer; @end @implementation Rootviewcontroller

Call:

-(void) viewdidload {    [super viewdidload];            5. Call Player        //Play network video    nsstring *urlstring = @ "Http://video.szzhangchu.com/qiaokeliruanxinbudingA.mp4";    Play local view, find the path to the file    //    NSString *urlstr = [[NSBundle mainbundle] pathforresource:@ "Youku-Tang shake a shake to be cheated. mp4" OfType: NIL];        [Self creatempplayercontroller:urlstring];    }

Step Two: Create the player

-(void) Creatempplayercontroller: (NSString *) string{   //1. Initialize player        //Prepare URL//     nsurl *urlstring = [Nsurl Fileurlwithpath:filenamepath];     Nsurl *urlstring = [Nsurl urlwithstring:string];        Initialize player   self.movieplayer = [[MPMoviePlayerController alloc] initwithcontenturl:urlstring];       Ready to play//    [_movieplayer preparetoplay];    Set MoviePlayer frame    _movieplayer.view.frame = self.view.frame;    Add to Parent view    [Self.view Addsubview:_movieplayer.view];            2. Configuration Properties    //Whether AutoPlay, default is no    _movieplayer.shouldautoplay = yes;    Set the style of the player    [_movieplayer Setcontrolstyle: (Mpmoviecontrolstylefullscreen)];    Start playing    [_movieplayer play];            3. Registration notice    //Registration Notice    [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (moviefinshed :) name:mpmovieplayerplaybackdidfinishnotification Object:nil];        }

Step Three: Implement the notification method

4. Implement the method in the notification-(void) moviefinshed: (nsnotification *) sender{    //Take out the notification center MoviePlayer    MPMoviePlayerController * Movie = [Sender object];        Remove Observer    [[Nsnotificationcenter Defaultcenter] removeobserver:self name: Mpmovieplayerplaybackdidfinishnotification Object:nil];    Move movie out of Parent view    [Movie.view Removefromsuperview];    }

Final effect:



follow the daily new technology of Bo Master Weibo: Http://weibo.com/hanjunqiang

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Mpmovieplayer Implementation video Audio player in iOS Author: Han Junqiang

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.