SoFunction
Updated on 2025-03-10

Detailed explanation of the usage of components in iOS apps for playing local videos and selecting local audio

MPMoviePlayerControlle plays local videos

MPMoviePlayerController is somewhat similar to AVAudioPlayer. The former plays videos and the latter plays audios, but there are also big differences. MPMoviePlayerController can be initialized directly through a remote URL, while AVAudioPlayer cannot. But it feels similar to use in general. Less nonsense and enter the experience.
The format supports: MOV, MP4, M4V, and 3GP, and supports a variety of audio formats.
First you have to introduce . and then import the corresponding header file in the file used to MPMoviePlayerController.

1. Create
The MPMoviePlayerController class is initialized through an NSURL, which can be local or remote. Initialization needs to be implemented through the initWithContentURL method:

Copy the codeThe code is as follows:

MPMoviePlayerController *moviePlayer = [ [ MPMoviePlayerController alloc]initWithContentURL:[NSURL urlWithString:@"http://"] ];//Remote

or
Copy the codeThe code is as follows:

NSString* path =[ NSString stringWithFormat:@"%@/Documents/video.3gp",NSHomeDirectory()];//Local path
MPMoviePlayerController *moviePlayer = [ [ MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];//Local

2. Properties settings
1. Controller style

Copy the codeThe code is as follows:

= MPMovieControlModeDefault; 

The following styles can be used:
MPMovieControlModeDefault                                                                                                                         �
MPMovieControlModeVolumeOnly                  Show volume control only
MPMovieControlModeHidden                                                         �
2. Screen aspect ratio
Copy the codeThe code is as follows:

= MPMovieScallingModeAspectFit; 

You can use the following aspect ratios:
MPMovieScallingModeNone
MPMovieScallingModeAspectFit                                                                                                                        �
MPMovieScallingModeAspectFill                                                                                                                        �
MPMovieScallingModeFill
3. Background color
The background color will be used when the movie player is transferred in and out, and when the movie cannot fill the entire screen, it will also be used to fill in the blank area. The default background color is black, but you can use the UIColor object to set the backgroundColor property to change the background color:
Copy the codeThe code is as follows:

= [UIColor redColor]; 

3. Play and stop movies
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:

Copy the codeThe code is as follows:

[ moviePlayer play ];

When the user clicks the Done button, or the stop method is called, it will stop
Copy the codeThe code is as follows:

[ moviePlayer stop ]; 

When the movie stops playing, it will automatically switch back to the view where the application was located before the playback.

4. Notification
Your program can configure when the movie player sends notifications, including ending loading content, technical playback, changing aspect ratio, etc. The movie player sends events to Cocoa's notification center, which you can configure to specify an object that forwards these events to your application. To receive these notifications, you need to use the NSNotificationCenter class to add an observer to the movie player:

Copy the codeThe code is 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 parameters let you know which event triggered the delegate method:
Copy the codeThe code is as follows:

-(void)moviePlayerPreloadDidFinish:(NSNotification*)notification{ 
//Add your processing code
}  

You will observe the following notification:

Issued when the movie player finishes preloading of the content. Because the content can be played with only a portion loaded, this notification may not be issued after it has been played.

Issue when the user changes the zoom mode of the movie. Users can touch the zoom icon to switch between full screen playback and window playback.

When the movie is played or the user presses the Done button, it is sent.

MPMediaPickerController selects local sound

MPMediaPickerController is similar to UIImagePickerController, allowing users to select music, podcasts, and audio books from the music library.
1. Create

Copy the codeThe code is as follows:

MPMediaPickerController *mpc = [[MPMediaPickerControlleralloc]initWithMediaTypes:MPMediaTypeMusic]; 
= self;//Trust
=@"Please select a music";// Prompt text
=NO;//Is it allowed to select multiple options at a time
 
The above code creates an MPMediaPickerController and sets the relevant properties. There is a parameter during initialization that is the media type, and the media type can be the following values:
Copy the codeThe code is 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 the codeThe code is 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 handle the selected content. The following function is responsible for handling the actions that are canceled after selection:
Copy the codeThe code is as follows:

-(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{ 
    /*insert your code*/ 
    [selfdismissModalViewControllerAnimated:YES]; 
    [mediaPicker release]; 
}

3. Display
You can call the following code whenever you need to display:
Copy the codeThe code is as follows:

[selfpresentModalViewController:mpc animated:YES]; 

4. Key points
After reading the above code, you may understand it, but you don’t feel it. Why? You will know by looking at the first callback function. It seems that I don’t know the callback function parameters. The items of the object of MPMediaItemCollection are the collection of user selections. Each item is a member of the MPMediaItem class and can query its attribute values. There are too many attributes, so I won't list them one by one. You can understand by looking at the header file or official documentation of the MPMediaItem class.