iOS Development MPMoviePlayerController Video player

Source: Internet
Author: User

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: 
    1. MPMoviePlayerController *movieplayer = [[MPMoviePlayerController alloc]initwithcontenturl:[nsurl urlWithString:@] HTTP//"]];//remote
Or 
    1. nsstring* path =[nsstring stringwithformat:@ "%@/documents/video.3gp", Nshomedirectory ()];//Local Path
    2. MPMoviePlayerController *movieplayer = [[MPMoviePlayerController alloc]initwithcontenturl:[nsurl FileURLWithPath: path]];//the Local
Second, property settings 
1. Controller style 
    1. 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 
    1. 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: 
    1. 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: 
    1. [MoviePlayer play];
When the user clicks the Done button, or the Stop method is called, it will stop 
    1. [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: 
    1. nsnotificationcenter* notificationcenter = [Nsnotificationcenter defaultcenter];
    2. [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: 
    1. -(void) Movieplayerpreloaddidfinish: (nsnotification*) notification{
    2. Add your processing code
    3. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.