Explain the component usage in IOS applications that play local video and select local audio _ios

Source: Internet
Author: User
Tags notification center

Mpmovieplayercontrolle play local video

Mpmovieplayercontrolle is a bit like Avaudioplayer, which plays video, which plays audio, but there are a lot of differences, MPMoviePlayerController can be initialized directly via a remote URL, And Avaudioplayer is not allowed. But basically it feels pretty much the same. Less nonsense to enter the experience.
Format support: MOV, MP4, M4V, and 3GP formats, but also support a variety of audio formats.
First you have to introduce mediaplayer.framework. Then import the corresponding header file in the file used to MPMoviePlayerController.

First, create
the MPMoviePlayerController class is initialized with a nsurl, which can be either local or remote. Initialization needs to be implemented through the Initwithcontenturl method:

Copy Code code as follows:

MPMoviePlayerController *movieplayer = [[MPMoviePlayerController alloc]initwithcontenturl:[nsurl urlWithString:@] http://"]];//remote

Or
Copy Code code as follows:

Second, property settings
1. Controller style

Copy Code code as follows:

Movieplayer.moviewcontrolmode = Mpmoviecontrolmodedefault;

You can use the following styles:
Mpmoviecontrolmodedefault Display Play/pause, volume, and Time control
Mpmoviecontrolmodevolumeonly only show Volume control
Mpmoviecontrolmodehidden No Controller
2. Screen width and height ratio
Copy Code code as follows:

Movieplayer.scallingmode = Mpmoviescallingmodeaspectfit;

You can use the following width and height ratios:
Mpmoviescallingmodenone does not do any scaling
Mpmoviescallingmodeaspectfit adapt to screen size, maintain the ratio of width to height
Mpmoviescallingmodeaspectfill adapt to screen size, maintain width and height ratio, can be cropped
Mpmoviescallingmodefill full screen, do not maintain a wide-height ratio
3. Background color
The background color is used when the movie player turns out and fills the blank area when the movie cannot fill the entire screen. The default background color is black, but you can use the Uicolor object to set the BackgroundColor property to change the background color:
Copy Code code as follows:

Movieplayer.backgroundcolor = [Uicolor Redcolor];

Third, play and stop the movie
to play a movie, call the Play method, and the movie playback controller automatically switches the view to the movie player and starts playing:

Copy Code code as follows:

[MoviePlayer play];

When the user clicks the Done button, or the Stop method is invoked, it stops
Copy Code code as follows:

[MoviePlayer stop];

When the movie stops playing, it automatically cuts back to the view in which the application is located before playback.

IV. notification
Your program can configure the movie player when to send notifications, including ending loading content, technology playback, changing aspect ratio, and so on. The movie player sends the event to the COCOA Notification Center, and you can configure it 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:

Copy Code code as follows:

nsnotificationcenter* notificationcenter = [Nsnotificationcenter defaultcenter];
[Notificationcenter addobserver:self selector: @selector (movieplayerpreloadfinish:) Name: Mpmovieplayercontentpreloaddidfinishnotification Object:movieplayer];

The notification will be sent to the delegate class and target method you specified. The notification parameter allows you to know which event triggered the delegate method:
Copy Code code as follows:

-(void) Movieplayerpreloaddidfinish: (nsnotification*) notification{
Add your processing code
}

You will observe the following notices:
1.MPMoviePlayerContentPreloadDidFinishNotification
Emitted when the movie player finishes preload on the content. Because the content can be played with only a fraction of the load, the notification may not be issued until it has been played.
2.MPMoviePlayerScallingModeDidChangedNotification
Issued when the user changes the zoom mode of the movie. The user can click the zoom icon to toggle between Full-screen playback and window playback.
3.MPMoviePlayerPlaybackDidFinishNotification
When the movie has finished playing or the user presses the Done button.

Mpmediapickercontroller Select local tone

Mpmediapickercontroller is similar to Uiimagepickercontroller, allowing users to select music, podcasts, and audio books from the music library.
1, create

Copy Code code as follows:

Mpmediapickercontroller *MPC = [[Mpmediapickercontrolleralloc]initwithmediatypes:mpmediatypemusic];
Mpc.delegate = self;//Delegate
Mpc.prompt =@ "Please select a music";/hint text
mpc.allowspickingmultipleitems=no;//whether to allow multiple selections at once

The code above creates a mpmediapickercontroller and sets the associated properties. When initialized, one of the parameters is the media type, and the media type can be the following values:
Copy Code code as follows:

enum {
Audio
Mpmediatypemusic = 1 << 0,
Mpmediatypepodcast = 1 << 1,
Mpmediatypeaudiobook = 1 << 2,
Mpmediatypeaudioitunesu = 1 << 3,//Available in IOS 5.0
Mpmediatypeanyaudio = 0x00ff,

Video (available in IOS 5.0)
Mpmediatypemovie = 1 << 8,
Mpmediatypetvshow = 1 << 9,
Mpmediatypevideopodcast = 1 << 10,
Mpmediatypemusicvideo = 1 << 11,
Mpmediatypevideoitunesu = 1 << 12,
Mpmediatypeanyvideo = 0xff00,

Mpmediatypeany = ~0
};
typedef Nsinteger MPMEDIATYPE;

2. Delegate function
Copy Code code as follows:

-(void) Mediapicker: (Mpmediapickercontroller *) Mediapicker didpickmediaitems: (mpmediaitemcollection *) mediaitemcollection{
/*insert your code*/
for (mpmediaitem* itemin [mediaitemcollection items]) {
}
[Selfdismissmodalviewcontrolleranimated:yes];
[Mediapicker release];
}

In the above function you can work with the selected content. The following function is responsible for handling actions that are canceled when checked:
Copy Code code as follows:

-(void) Mediapickerdidcancel: (Mpmediapickercontroller *) mediapicker{
/*insert your code*/
[Selfdismissmodalviewcontrolleranimated:yes];
[Mediapicker release];
}

3, display
You can call the following code at any time you need to display it:
Copy Code code as follows:

[SELFPRESENTMODALVIEWCONTROLLER:MPC Animated:yes];

4, Key points
Read the code above and you may understand, but you don't get it. Why, then? Look at the first callback function to know that the callback function is not known as a reference. The items of the Mpmediaitemcollection object are the collection of user-selected items. Each item is a member of the Mpmediaitem class and can query its property values. There are too many attributes, I will not list them, you can see the Mpmediaitem class header file or official document to understand.

Related Article

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.