1.申請媒體庫使用權(quán)限
+ (BOOL)hasMediaLibraryAuth {
? ? MPMediaLibraryAuthorizationStatus aStatus = [MPMediaLibrary authorizationStatus];
? ? if (aStatus == MPMediaLibraryAuthorizationStatusNotDetermined) {
? ? ? ? [MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status) {
? ? ? ? ? ? if (aStatus == MPMediaLibraryAuthorizationStatusAuthorized) {
? ? ? ? ? ? ? ? [self fetchItunsMusics];
? ? ? ? ? ? }
? ? ? ? }];
? ? ? ? return NO;
? ? }
? ? if (aStatus == MPMediaLibraryAuthorizationStatusAuthorized) {
? ? ? ? returnYES;
? ? }else{
? ? ? ? returnNO;
? ? }
}
2.獲取本地媒體庫的音樂列表
+ (NSArray *)fetchItunsMusics {
? ? if (![self hasMediaLibraryAuth]) {
? ? ? ? return@[];
? ? }
? ??MPMediaQuery *query = [[MPMediaQuery alloc] init];
? ? MPMediaPropertyPredicate *albumNamePredicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber? ? ? ?????????numberWithInt:MPMediaTypeMusic] forProperty:MPMediaItemPropertyMediaType];
? ??[query addFilterPredicate:albumNamePredicate];
? ??NSArray*a = [query items];
? ? return a;
}
3.獲取播放器實(shí)例
MPMusicPlayerApplicationController*musicPlayer = [MPMusicPlayerController applicationQueuePlayer];
4.將播放隊(duì)列傳給播放器
[musicPlayer setQueueWithQuery:query];
5.調(diào)用播放接口
[musicPlayer play];
6.切換歌曲
//記錄下列表中的?MPMediaItem實(shí)例
MPMediaItem*mi = [self.musicItemsMap objectForKey:mid];
[musicPlayer setNowPlayingItem:mi];
[musicPlayer play];