This thing is similar to the previous audio playback, it is also necessary to import the system framework Mediaplayer.framework before you can use the MPMoviePlayerController file to import the corresponding header file
Initialize: There's something different here. MPMoviePlayerController can be initialized via a remote URL, for example:
MPMoviePlayerController *movieplayer = [[MPMoviePlayerController alloc]initwithcontenturl:[nsurl urlWithString:@] URL "]];
Next is the controller style setting:
Movieplayer.moviewcontrolmode = Mpmoviecontrolmodehidden;
You can use the following styles:
Mpmoviecontrolmodedefault Show Play/Pause, volume and time control
Mpmoviecontrolmodevolumeonly only show Volume control
Mpmoviecontrolmodehidden No Controller
Normally, we usually define the video player by ourselves, so most of them choose the one with no controller at the end.
Screen width high ratio:
Movieplayer.scallingmode = Mpmoviescallingmodenone;
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
[MoviePlayer play]; Start playback [movieplayer stop]; Stop playing
Register a notification your program can configure when the movie player sends notifications, including ending loading content, technology playback, change aspect ratio, and so on. The movie player sends events to the COCOA notification hub, which you can configure to specify an object to forward these events to your application. To receive these notifications, you need to use the Nsnotificationcenter class to add an observer (Observer) to the movie player:
nsnotificationcenter* notificationcenter = [Nsnotificationcenter defaultcenter]; [Notificationcenter addobserver:self selector: @selector (movieplayerpreloadfinish:) Name: Mpmovieplayercontentpreloaddidfinishnotification object:movieplayer];//Notification will be sent to the delegate class and target method you specified. The notification parameter allows you to know which event triggered the delegate method:-(void) Movieplayerpreloaddidfinish: (nsnotification*) notification{ //Add your processing code }
You will observe the following notifications:
Mpmovieplayercontentpreloaddidfinishnotification
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
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 has finished playing or the user presses the Done button.
Here's a small example: custom video gestures to control video progress and volume size
#import <MediaPlayer/MediaPlayer.h> @interface kkbmovieplayercontroller:mpmovieplayercontroller< Uigesturerecognizerdelegate> @end
#import "KKBMoviePlayerController.h" #import "AppDelegate.h" @interface Kkbmovieplayercontroller () {BOOL _INFULLSCR Een Uipangesturerecognizer *_pan; Cgpoint _lastpoint; BOOL _startchange; BOOL _changevolume; } @end @implementation Kkbmovieplayercontroller-(ID) Initwithcontenturl: (Nsurl *) url{self =[super initwith Contenturl:url]; if (self) {self.view.backgroundColor = [uicolor Clearcolor]; Self.initialplaybacktime =-1; Self.endplaybacktime =-1; [Self preparetoplay]; [Self play]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selecto R (enterfullscreen:) name:mpmovieplayerwillenterfullscreennotificatio n Object:nil]; [[Nsnotificationcenter Defaultcenter] Addobserver:self Selector: @selector (leavefullscreen:) Name:mpmovieplayerwillexitfullscreennotification o Bject:nil]; } return self; } #pragma mark-full screen controller-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrec Eivetouch: (Uitouch *) touch{return YES; }-(void) Handlepan: (uipangesturerecognizer*) rec{if (_infullscreen) {if (rec.state = = Uigesturerecogniz Erstatebegan) {_lastpoint = [rec LocationInView:self.view]; } else if (rec.state = = uigesturerecognizerstatechanged) {cgpoint nowpoint = [rec LocationInView:self.view]; if (_startchange = = NO) {if (Fabs (Nowpoint.y-_lastpoint.y) > Fabs (nowpo Int.x-_lastpoint.x)) {_changevolume = NO; } else { _changevolume = YES; } _startchange = YES; } else {if (_changevolume) {//change volume float volume = [[ Mpmusicplayercontroller Applicationmusicplayer] volume]; float newvolume = volume; if (nowpoint.x = = _lastpoint.x) {} else {if (now Point.x < _lastpoint.x) {newvolume + = 0.01; } else {newvolume-= 0.01; }} if (Newvolume < 0) {NEWVO Lume = 0; } else if (Newvolume > 1.0) {newvolume = 1.0; } [[Mpmusicplayercontroller appLicationmusicplayer] Setvolume:newvolume]; } else {//change playback state if (self.playbackstate! = Mpmovieplaybackstates Eekingforward && self.playbackstate! = Mpmovieplaybackstateseekingbackward) { if (Nowpoint.y = = _lastpoint.y) {} else { if (Nowpoint.y < _LASTPOINT.Y) {[Self beginseekingforward]; } else {[self beginseekingbackward]; }} _lastpoint = Nowpoint; }}}} else if (rec.state = = Uigesturerecognizerst atecancelled | | Rec.state = = Uigesturerecognizerstateended | | Rec.staTe = = uigesturerecognizerstatefailed) {_startchange = NO; [Self endseeking]; }}}-(void) Enterfullscreen: (nsnotification*) notification{_infullscreen = YES; _pan = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (Handlepan:)]; _pan.delegate = self; [[[[UIApplication sharedapplication] windows] objectatindex:0] addgesturerecognizer:_pan]; }-(void) Leavefullscreen: (nsnotification*) notification{_infullscreen = NO; [[[[UIApplication sharedapplication] windows] objectatindex:0] removegesturerecognizer:_pan]; } @end
Original: http://blog.csdn.net/u013561113/article/details/21457903
iOS Video player MPMoviePlayerController