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