In iOS, MPMoviePlayer implements video and audio playback by Han junqiang
For ios video files, MPMoviePlayerViewController and MPMoviePlayerController are generally used. The former is a view, and the latter is a Controller. The difference is that MPMoviePlayerViewController contains an MPMoviePlayerController.
Note: MPMoviePlayerViewController must be added in the presentMoviePlayerViewControllerAnimated mode. Otherwise, the Done button will not respond to the notification MPMoviePlayerPlaybackDidFinishNotification event;
First, include # import Header file and MediaPlayer. framework.
MPMovieControlModeDefault: Display playing/pause, volume and time control
MPMovieControlModeVolumeOnly only displays volume control
MPMovieControlModeHidden no Controller
You can use the following width/height ratio:
MPMovieScallingModeNone does not scale
MPMovieScallingModeAspectFit adapts to the screen size and maintains the aspect ratio.
MPMovieScallingModeAspectFill adapts to the screen size, maintains the aspect ratio, and supports cropping.
MPMovieScallingModeFill fills the screen and does not maintain the aspect ratio.
// Notification
MPMoviePlayerContentPreloadDidFinishNotification is sent when the movie player finishes pre-loading the content. Because the content can be played only when a part of the content is loaded, this notification may be sent only after it has been played.
MPMoviePlayerScallingModeDidChangedNotification is triggered when the user changes the scaling mode of the movie. You can click the zoom icon to switch between full-screen playback and window playback.
MPMoviePlayerPlaybackDidFinishNotification
========================================================== ========================================================
Framework to be introduced: MediaPlayer. framework
Step 1: Introduce framework settings
#import RootViewController.h#import
@interface RootViewController ()@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;@end@implementation RootViewController
Call:
-(Void) viewDidLoad {[super viewDidLoad]; // 5. call the player // play the network video NSString * urlString = @ brief // play the local view, find the file path // NSString * urlStr = [[NSBundle mainBundle] pathForResource: @ Youku-Tang Doudou shake it to death. MP4 ofType: nil]; [self createMPPlayerController: urlString];}
Step 2: Create a player
-(Void) createMPPlayerController :( NSString *) string {// 1. initialize the player // prepare the URL // NSURL * urlString = [NSURL fileURLWithPath: fileNamePath]; NSURL * urlString = [NSURL URLWithString: string]; // initialize the player self. moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: urlString]; // prepare for playback // [_ moviePlayer prepareToPlay]; // set the moviePlayer frame _ moviePlayer. view. frame = self. view. frame; // Add to parent view [self. view addSubview: _ moviePlayer. view]; // 2. configure the property // whether to play the video automatically. The default value is NO _ moviePlayer. shouldAutoplay = YES; // set the player style [_ moviePlayer setControlStyle :( MPMovieControlStyleFullscreen)]; // start playing [_ moviePlayer play]; // 3. registration notification // registration notification [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (movieFinshed :) name: MPMoviePlayerPlaybackDidFinishNotification object: nil];}
Step 3: Implement notification methods
// 4. method of implementing notification-(void) movieFinshed :( NSNotification *) sender {// retrieve moviePlayer MPMoviePlayerController * movie = [sender object] of the notification center; // remove the observer [[NSNotificationCenter defacenter center] removeObserver: self name: MPMoviePlayerPlaybackDidFinishNotification object: nil]; // remove the movie from the parent view [movie. view removeFromSuperview];}
Final effect: