MPMediaQuery *mediaQueue = [MPMediaQuery artistsQuery];
// collection 是一种集合;
for (MPMediaItemCollection * collection in mediaQueue.collections) {
NSLog(@"%ld",mediaQueue.collections.count);
for (MPMediaItem *item in collection.items) {
NSLog(@"%@",item.title);
}
NSLog(@"-------------");
}
申明一个Collection便于下面给musicPlayerController赋值
// 转换成mp4格式,目前mp4格式不能转码成mp3,此方法废弃
+ (void)convertItuneMusicToMp4WithMediaItem: (MPMediaItem*)item complete:(void(^)(MPMediaItem *item,NSInteger status))complete
{
NSURL*url = [item valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset*songAsset = [AVURLAsset URLAssetWithURL:url options:nil];
NSFileManager*fileManager = [NSFileManager defaultManager];
NSLog(@"compatible presets for songAsset: %@",[AVAssetExportSession exportPresetsCompatibleWithAsset:songAsset]);
NSArray*ar = [AVAssetExportSession exportPresetsCompatibleWithAsset: songAsset];
NSLog(@"%@", ar);
AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
initWithAsset: songAsset
presetName:AVAssetExportPresetAppleM4A];
NSLog(@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
exporter.outputFileType=@"com.apple.m4a-audio";
NSString*exportFile = [self filePathForMediaItem:item fileFormate:1]; // 1 = m4a formate
NSError*error1;
if([fileManager fileExistsAtPath:exportFile]) {
[fileManager removeItemAtPath:exportFile error:&error1];
}
NSURL * urlPath= [NSURL fileURLWithPath:exportFile];
exporter.outputURL=urlPath;
NSLog(@"---------%@",urlPath);
// export to filepath m4a
[exporter exportAsynchronouslyWithCompletionHandler:^ {
int exportStatus = exporter.status;
complete(item,exportStatus);
switch(exportStatus) {
case AVAssetExportSessionStatusFailed: {
// log error to text view
NSError*exportError = exporter.error;
NSLog(@"AVAssetExportSessionStatusFailed: %@", exportError);
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog(@"AVAssetExportSessionStatusCompleted");
break;
}
case AVAssetExportSessionStatusUnknown: {
NSLog(@"AVAssetExportSessionStatusUnknown");
break;
}
case AVAssetExportSessionStatusExporting: {
NSLog(@"AVAssetExportSessionStatusExporting");
break;
}
case AVAssetExportSessionStatusCancelled: {
NSLog(@"AVAssetExportSessionStatusCancelled");
break;
}
case AVAssetExportSessionStatusWaiting: {
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
}
default:
{NSLog(@"didn't get export status");
break;
}
}
}];
}