Detail the component usage of iOS application for playing local video and selecting local audio

  • 2020-06-12 10:41:56
  • OfStack

MPMoviePlayerControlle plays local videos

MPMoviePlayerControlle is similar to AVAudioPlayer in that it plays video and audio, but with a big difference. MPMoviePlayerController can be initialized directly via remote URL, while AVAudioPlayer cannot. But in general it feels the same. Cut the crap and get into the experience.
Format support: MOV, MP4, M4V, 3GP and other formats, but also support a variety of audio formats.
First you have to import MediaPlayer.framework. Then you have to import the corresponding header file in the file used in MPMoviePlayerController.

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


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

or

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

2. Property setting
1. Controller style


moviePlayer.moviewControlMode = MPMovieControlModeDefault; 

The following styles can be used:
MPMovieControlModeDefault shows play/pause, volume, and time controls
MPMovieControlModeVolumeOnly only displays volume control
MPMovieControlModeHidden has no controller
2. Screen width and height ratio

moviePlayer.scallingMode = MPMovieScallingModeAspectFit; 

You can use the following width-height ratios:
MPMovieScallingModeNone does not do any scaling
The MPMovieScallingModeAspectFit ADAPTS to the screen size and maintains the aspect ratio
The MPMovieScallingModeAspectFill fits the screen size, maintains the aspect ratio and can be cropped
The MPMovieScallingModeFill fills the screen without maintaining aspect ratio
3. The background color
The background color is used when the movie player is moving in and out, and when the movie does not fill the entire screen, it is also used to fill in empty areas. The default background color is black, but you can use the UIColor object to set the backgroundColor property to change the background color:
 
moviePlayer.backgroundColor = [UIColor redColor]; 

3. Play and stop the movie
To play the movie, call the play method, and the movie Player controller will automatically switch the view to the movie player and start playing:


[ moviePlayer play ];

It stops when the user clicks the Done button or when the stop method is called

[ moviePlayer stop ]; 

When the movie stops playing, it automatically cuts back to the view where the application was before playing.

4. Notice
Your program can configure when the movie player will send notifications, including ending load content, technology playback, changing aspect ratios, and more. The movie player will send events to Cocoa's notification center, which you can configure to specify an object to forward these events to your application. To receive these notifications, add an observer for the movie player (observer) using the NSNotificationCenter class:


NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter]; 
[ notificationCenter addObserver:self selector:@selector(moviePlayerPreloadFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:moviePlayer ]; 

The notification will be sent to the delegate class and the target method that you specify. The notification parameter lets you know which event triggered the delegate method:

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

You will notice the following:
1.MPMoviePlayerContentPreloadDidFinishNotification
Sends when the movie player finishes preloading the content. Because the content can be played with only 1 part loaded, this notification may be sent after it has already been played.
2.MPMoviePlayerScallingModeDidChangedNotification
Sends when the user changes the movie's zoom mode. Users can tap the zoom icon to switch between full-screen play and window play.
3.MPMoviePlayerPlaybackDidFinishNotification
When the movie is finished or the user presses the Done button.

MPMediaPickerController selects the local tone

MPMediaPickerController, like UIImagePickerController, allows users to select music, podcasts, and audio books from their music library.
1, create,


MPMediaPickerController *mpc = [[MPMediaPickerControlleralloc]initWithMediaTypes:MPMediaTypeMusic]; 
   mpc.delegate = self;// entrust  
   mpc.prompt =@"Please select a music";// Prompt words  
   mpc.allowsPickingMultipleItems=NO;// Whether to allow 1 Multiple secondary selections

The above code creates an MPMediaPickerController and sets the associated properties. One parameter is the media type when initialized, and the media type can be the following values:

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 functions

- (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 selection. The following function handles the check and cancel action:

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

3, display
You can call the following code whenever you want to display:

[selfpresentModalViewController:mpc animated:YES]; 

4. Key points
After reading the above code, you may understand it, but you don't feel like you understand it. Why is that? If you look at the first callback function, I don't know what the callback function is. The items object of MPMediaItemCollection is the collection of user choices. Each item is a member of the MPMediaItem class and can be queried for attribute values. There are too many properties, I won't list them in 11, you can just look at the header file or the official document of the MPMediaItem class.


Related articles: