IOS provides the MPMoviePlayerController MPMoviePlayerViewController class, which can be easily used for video playback. MPMoviePlayerViewController can only play videos in full screen mode.
# Import "MainViewController. h" # import
@ Interface MainViewController () // Video player @ property (strong, nonatomic) MPMoviePlayerController * player; @ property (strong, nonatomic) UIImageView * imageView; @ end @ implementation MainViewController-(void) viewDidLoad {[super viewDidLoad]; // instantiate the video player NSURL * url = [[NSBundle mainBundle] URLForResource: @ "promo_full" withExtension: @ "mp4"]; // video playback is the playing mode of streaming media. The so-called streaming media means to load and play video data like a stream. //// Prompt: If the url contains Chinese characters, add a percent sign. // NSString * urlString = @ "http: www.xxx.com/video/xxx.mp4"; // NSURL * url = [NSURL URLWithString: [urlString encoding: NSUTF8StringEncoding]; self. player = [[MPMoviePlayerController alloc] initWithContentURL: url]; // 1 set the player size [self. player. view setFrame: CGRectMake (0, 0,320,180)]; // is the mainstream media style // 2 Add the player view to the root view [self. view addSubview: self. player. view]; // 4 play [self. playe R play]; // [self. player stop]; // listens to the video playback status in observer mode through the notification center. // 1 listens to the playback status [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (stateChange) name: jsonobject: nil]; // 2 listener playback completed [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (finishedPlay) name: MPMoviePlayerPlaybackDidFinishNotification object: nil]; // 3 video [[NSNotificationC Enter defaultCenter] addObserver: self selector: @ selector (caputerImage :) name: jsonobject: nil]; // 3 video [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (caputerImage :) name: MPMoviePlayerThumbnailImageRequestDidFinishNotification object: nil]; // 4 exit full screen notification [[nsicationicationcenter defacenter] addObserver: self selector: @ selector (exit FullScreen) name: MPMoviePlayerDidExitFullscreenNotification object: nil]; // asynchronous video, which can be specified at attimes. [Self. player preview: @ [@ 10.0f, @ 20.0f] timeOption: MPMovieTimeOptionNearestKeyFrame]; UIImageView * thumbnailImageView = [[UIImageView alloc] initWithFrame: CGRectMake (80,200,160, 90. imageView = thumbnailImageView; [self. view addSubview: thumbnailImageView] ;}# pragma mark exit full screen-(void) exitFullScreen {NSLog (@ "exit full screen ");} # pragma mark-player event listening # This method of pragma mark video is asynchronous-(void) cap UterImage :( NSNotification *) notification {NSLog (@ "% @", notification); UIImage * image = notification. userInfo [@ "MPMoviePlayerThumbnailImageKey"]; [self. imageView setImage: image] ;}# pragma mark player event listener # pragma mark playback completed-(void) finishedPlay {NSLog (@ "playback completed ");} # pragma mark player video monitoring # Changes in the playing status of The pragma mark/* MPMoviePlaybackStateStopped, // stop MPMoviePlaybackStatePlaying, // play MPMoviePlaybackStatePaused, // pause the MP MoviePlaybackStateInterrupted, // interrupt MPMoviePlaybackStateSeekingForward, // fast forward to MPMoviePlaybackStateSeekingBackward */-(void) stateChange {switch (self. player. playbackState) {case MPMoviePlaybackStatePaused: NSLog (@ "pause"); break; case MPMoviePlaybackStatePlaying: // set full-screen playback [self. player setFullscreen: YES animated: YES]; NSLog (@ "play"); break; case MPMoviePlaybackStateStopped: // Note: The MPMovie is not triggered after normal playback. PlaybackStateStopped event. // Call the [self. player stop] method to trigger this event. NSLog (@ "stop"); break; default: break ;}}@ end