Playing system music by UIMPMediaPickerController in iOS

Source: Internet
Author: User

Playing system music by UIMPMediaPickerController in iOS

The layout is as follows:

 

Introduction framework:

# Import

# Import

Follow the protocol and introduce header files

 

#import ViewController.h#import #import 
 
  @interface ViewController ()
  
   @end@implementation ViewController
  
 
Click Event:

 

 

-(IBAction) pickMusicAction :( UIButton *) sender {// create an MPMediaPickerController and specify the media type for it // media type: including music, audio, etc. // MPMediaTypeMovie // MPMediaTypeMusic MPMediaPickerController * mediaPC = [[MPMediaPickerController alloc] initWithMediaTypes :( MPMediaTypeMusic)]; if (mediaPC! = Nil) {NSLog (@ Successfully instantiated a media picker); // set related attributes // proxy mediaPC. delegate = self; // The prompt text mediaPC. prompt = @ select the music to be played; // whether multiple mediaPC files can be selected at a time. allowsPickingMultipleItems = YES; [self presentViewController: mediaPC animated: YES completion: nil];} else {NSLog (@ cocould not instantiate a media picker );}}

 

// Obtain the selected song using the proxy method

// MPMediaItemCollection multimedia item set

// MPMediaItem a single multimedia item, such as a song

// Collection is a set of Ordered items.

-(Void) mediaPicker :( MPMediaPickerController *) mediaPicker didPickMediaItems :( MPMediaItemCollection *) mediaItemCollection {NSLog (, mediaItemCollection ); // MPMusicPlayerController can play music in the music library. // MPMusicPlayerController provides two types of players: applicationMusicPlayer and iPodMusicPlayer. iPodMusicPlayer is used here. The former automatically stops playing music after the application exits, and the latter does not exit playing after the application stops. MPMusicPlayerController * musicPC = [[MPMusicPlayerController alloc] init]; // MPMusicPlayerController loads music differently from AVAudioPlayer. AVAudioPlayer loads music in a file path, while MPMusicPlayerController requires a playing queue, it is precisely because its source of playing audio is a queue that MPMusicPlayerController supports operations such as the previous and next songs. [MusicPC setQueueWithItemCollection: mediaItemCollection]; [musicPC play];}

// Cancel the action after selection-(void) mediaPickerDidCancel :( MPMediaPickerController *) mediaPicker {NSLog (@ Media Picker was canceled); [mediaPicker failed: YES completion: nil];}

Final effect:

 

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.