Uimpmediapickercontroller Play system Music in iOS

Source: Internet
Author: User

The layout is as follows:



Introduce the framework:

#import <AVFoundation/AVFoundation.h>

#import <MediaPlayer/MediaPlayer.h>

Adherence to protocols and introduction of header files

#import "ViewController.h" #import <AVFoundation/AVFoundation.h> #import <mediaplayer/mediaplayer.h>@ Interface Viewcontroller () <MPMediaPickerControllerDelegate> @end @implementation Viewcontroller
Click events:

-(Ibaction) Pickmusicaction: (UIButton *) Sender {        //Create a Mpmediapickercontroller and assign it a media type    //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 properties        //Agent        mediapc.delegate = self;        Hint text        mediapc.prompt = @ "Please select music to play";        Allow multiple Mediapc.allowspickingmultipleitems = YES to be selected at one time        ;                [Self PRESENTVIEWCONTROLLER:MEDIAPC animated:yes completion:nil];      } else{        NSLog (@ "Could not instantiate a media picker");    }        }

To get the selected songs by proxy method

Mpmediaitemcollection Multimedia Item Collection

Mpmediaitem a single multimedia item, such as a song

Collection is a set of ordered item collections

-(void) Mediapicker: (Mpmediapickercontroller *) Mediapicker didpickmediaitems: (mpmediaitemcollection *) mediaitemcollection{    NSLog (@ "%@", mediaitemcollection);        The Mpmusicplayercontroller class can play music in the music library    //mpmusicplayercontroller provides two types of player, one is Applicationmusicplayer, One is Ipodmusicplayer, here with Ipodmusicplayer. The former will automatically stop after the app exits, which will not exit when the app stops.    Mpmusicplayercontroller *MUSICPC = [[Mpmusicplayercontroller alloc]init];        Mpmusicplayercontroller loading music different from the previous avaudioplayer,avaudioplayer is loaded by a file path, and Mpmusicplayercontroller needs a play queue, It is because it is playing the audio source is a queue, so Mpmusicplayercontroller supports the previous, the next song and other operations.        [Musicpc setqueuewithitemcollection:mediaitemcollection];    [Musicpc play];   }

Cancel action When selected-(void) Mediapickerdidcancel: (Mpmediapickercontroller *) mediapicker{    NSLog (@ "Media Picker was Cancelled ");        [Mediapicker Dismissviewcontrolleranimated:yes Completion:nil];    }

Final effect:

Daily Update Attention :http://weibo.com/hanjunqiang Sina Weibo

Uimpmediapickercontroller Play system Music in iOS

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.