Multimedia play-video play, multimedia-video play
For video playback, You need to import the framework MediaPlayer. framework into the file # import <MediaPlayer/MediaPlayer. h>
There are two ways to play a video: MPMoviePlayerController. Although the name is controller, It inherits from NSObject and is not a view. It has a view attribute and can set the size.
The other is the video playback View Controller MPMoviePlayerViewController. A full-screen video playback interface is displayed, and the size cannot be changed.
Method 1: MPMoviePlayerController:
MPMoviePlayerController * movie = [[MPMoviePlayerController alloc] init]; _ movie. contentURL = url; _ movie. view. frame = CGRectMake (0, 20,375,550); // Add to window [self. view addSubview: _ movie. view]; // play [_ movie play];
After the video is played, movie. view will not be automatically removed. However, after the video is played, the system will generate a notification of playback completion, so we will create a notification center to obtain it.
// Register the notification and check whether the video has been played. [[nsnotifcenter center defacenter center] addObserver: self selector: @ selector (stopPlay) name: MPMoviePlayerPlaybackDidFinishNotification object: nil]; // notification method-(void) stopPlay {[_ movie. view removeFromSuperview];}
Method 2: MPMoviePlayerViewController:
MPMoviePlayerViewController * playerViewController = [[MPMoviePlayerViewController alloc] modes: url]; // bring up the video player UI mode and play it automatically [self presentViewController: playerViewController animated: YES completion: nil];
After the MPMoviePlayerViewController is played, it will automatically pop up and you do not need to manage it in the notification center.
Note: The url is a video link, both of which can directly play the network link.