Statement You are welcome to repost this article, but please respect the author's Labor achievements. repost this article and keep the statement in this box. Thank you. Article Source: http://blog.csdn.net/iukey |
Mpmovieplayercontroller is a bit similar to avaudioplayer. The former plays a video and the latter plays audio, but it is also very different. mpmovieplayercontroller can be initialized directly through a remote URL, but avaudioplayer cannot. But in general, it feels similar. Let's talk about the experience.
Supported formats: mov, MP4, m4v, and 3GP. Multiple audio formats are also supported.
First, you need to introduce mediaplayer. Framework. Then, import the corresponding header file in the file using mpmovieplayercontroller.
I. Create
The mpmovieplayercontroller class is initialized through an nsurl, which can be local or remote. Initialization must be implemented through the initwithcontenturl method:
Mpmovieplayercontroller * movieplayer = [[mpmovieplayercontroller alloc] initwithcontenturl: [nsurl urlwithstring: @ "http: //"]; // remote
Or
Nsstring * Path = [nsstring stringwithformat: @ "% @/documents/video.3gp", nshomedirectory ()]; // local path mpmovieplayercontroller * movieplayer = [[mpmovieplayercontroller alloc] initwithcontenturl: [nsurl fileurlwithpath: path]; // local
Ii. attribute settings
1. Controller Style
moviePlayer.moviewControlMode = MPMovieControlModeDefault;
You can use the following styles:
Mpmoviecontrolmodedefault: Display playing/pause, volume and time control
Mpmoviecontrolmodevolumeonly only displays volume control
Mpmoviecontrolmodehidden no Controller
2. Screen width/height ratio
moviePlayer.scallingMode = MPMovieScallingModeAspectFit;
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.
3. Background Color
The background color is used when the player is transferred in and out. When the screen cannot be filled, it is also used to fill the blank area. The Default background color is black, but you can use the uicolor object to set the backgroundcolor attribute to change the background color:
moviePlayer.backgroundColor = [UIColor redColor];
3. Play and stop movies
To play a movie, call the play method. The movie playback controller automatically switches the view to the movie player and starts playing the video:
[ moviePlayer play ];
When you click the done button or the stop method is called
[ moviePlayer stop ];
When a movie stops playing, it is automatically switched back to the view where the app is located.
Iv. Notifications
Your program can configure when the movie player sends notifications, including ending loading content, technical playback, and changing the aspect ratio. The movie player sends events to the cocoa notification center. You can configure these events to forward them to an object in your application. To receive these notifications, you must use the nsicationcenter center class to add an observer (observer) to the movie player ):
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];[ notificationCenter addObserver:self selector:@selector(moviePlayerPreloadFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:moviePlayer ];
The notification will be sent to the specified delegate class and target method. The notification parameters let you know which event triggers the delegate method:
-(Void) movieplayerpreloaddidfinish :( nsnotification *) Notification {// Add your processing code}
You will observe the following notifications:
Mpmovieplayercontentpreloaddidfinishnotification
After the content is pre-loaded, the video player sends the video. 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
When the user changes the zoom mode of the movie, it is triggered. You can click the zoom icon to switch between full-screen playback and window playback.
Mpmovieplayerplaybackdidfinishnotification
When the video is played or the user presses the done button.