MPMoviePlayerController and Avaudioplayer a bit similar, the former play video, the latter play audio, but also very different, MPMoviePlayerController can be directly initialized via remote URL, And Avaudioplayer is not allowed. But generally it feels about the same. Talk less about entering the experience. Format support: MOV, MP4, M4V, and 3GP format, also support a variety of audio formats.
First you have to introduce mediaplayer.framework. Then import the appropriate header file in the file that you used to MPMoviePlayerController.
First, create
The MPMoviePlayerController class is initialized by a nsurl, which can be either local or remote. Initialization needs to be implemented by means of 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]];//the Local
Second, property settings
1. Controller style
- Movieplayer.moviewcontrolmode = Mpmoviecontrolmodedefault;
You can use the following styles:
Mpmoviecontrolmodedefault Show Play/Pause, volume and time control
Mpmoviecontrolmodevolumeonly only show Volume control
Mpmoviecontrolmodehidden No Controller
2. Screen width and height ratio
- Movieplayer.scallingmode = Mpmoviescallingmodeaspectfit;
You can use the following width-to-height ratios:
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
3. Background color
The background color is used when the movie player is turned off and is used to fill the blank area when the movie doesn't fill the screen. The default background color is black, but you can change the background color by using the Uicolor object to set the BackgroundColor property:
- Movieplayer.backgroundcolor = [Uicolor Redcolor];
Third, play and stop the movie
To play a movie, call the Play method, and the movie playback controller will automatically switch the view to the movie player and start playing:
- [MoviePlayer play];
When the user clicks the Done button, or the Stop method is called, it will stop
- [MoviePlayer stop];
When the movie stops playing, it automatically cuts back to the view in which the application was pre-played.
Iv. Notice
Your program can configure when the movie player sends notifications, including ending loading content, technology playback, and changing aspect ratios. 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];
The notification is sent to the delegate class and the target method that 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.
iOS Development MPMoviePlayerController Video player