自iOS 8以来,iPad&iPhone设备返回一个空数组。当我从本地文件(NSBundle)中提取时,我可以获得专辑艺术或封面艺术,但从iTunes或设备本身购买的任何歌曲都是空的。
我已经更新到最新的XCode,两台设备上的最新iOS,以及iTunes。我在iPad 4、iPad Air、iPhone 5、iPhone 6上进行了测试。希望有人知道发生了什么,这似乎是iOS 8中的一个已知错误。此外,我可以播放资产并检索歌曲名称和艺术家等信息。
MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
NSArray *itemsFromGenericQuery = [songQuery items];
NSMutableArray *songsList = [[NSMutableArray alloc] initWithArray:itemsFromGenericQuery];
MPMediaItem *mediaItem = (MPMediaItem *)[songsList objectAtIndex:0];
NSURL *url = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
AVAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
NSArray *commonArray = [assets commonMetadata];
//Test A
NSArray *albumArray = [AVMetadataItem metadataItemsFromArray:commonArray filteredByIdentifier:AVMetadataIdentifieriTunesMetadataCoverArt];
NSLog(@"commonArray = %lu",(unsigned long)[commonArray count]); //Returns 3
NSLog(@"albumArray has %lu",(unsigned long)[albumArray count]); //Returns 0 or null
//Test B
for (AVMetadataItem *metadataItem in asset.commonMetadata) {
if ([metadataItem.commonKey isEqualToString:@"artwork"]){
NSDictionary *imageDataDictionary = (NSDictionary *)metadataItem.value;
NSData *imageData = [imageDataDictionary objectForKey:@"data"];
UIImage *image = [UIImage imageWithData:imageData];
coverArtImage.image = image;
}
}
//Test C
for (AVMetadataItem *item in commonArray) {
if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
NSData *newImage = [item.value copyWithZone:nil];
coverArtImage.image = [UIImage imageWithData:newImage];
}
}
//Test D
for (AVMetadataItem *item in asset.metadata) {
if ([item.commonKey isEqualToString:@"artwork"]){
NSDictionary *imageDataDictionary = (NSDictionary *)item.value;
NSData *imageData = [imageDataDictionary objectForKey:@"data"];
UIImage *image = [UIImage imageWithData:imageData];
coverArtImage.image = image;
}
if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
NSData *newImage = [item.value copyWithZone:nil];
coverArtImage.image = [UIImage imageWithData:newImage];
}
}
所有封面艺术图像都返回null或从未被调用。此外,按照苹果的建议,从直接文件中提取封面是异步的,但无论我在什么设备上尝试过,似乎都至少需要10秒。iOS 7允许我们直接从mediaItem中提取封面,而且它是即时的,我不明白他们为什么会削弱这个功能。
NSString *queryString = [url query];
if (queryString == nil) // shouldn't happen
return nil;
NSArray *components = [queryString componentsSeparatedByString:@"="];
if ([components count] < 2) // also shouldn't happen
return nil;
id trackId = [components objectAtIndex:1];
MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:trackId forProperty:MPMediaItemPropertyPersistentID]];
NSArray *items = [query items];
[query release];
if ([items count] < 1) // still shouldn't happen
return nil;
MPMediaItem* mpitem= [items objectAtIndex:0];
MPMediaItemArtwork* mpArt = [mpitem valueForProperty:MPMediaItemPropertyArtwork];
UIImage* mpImage = [mpArt imageWithSize:mpArt.bounds.size];
我建议尝试新的Swift实现:
let query = MPMediaQuery.songsQuery()
let mediaCollection = MPMediaItemCollection(items: query.items!)
print(mediaCollection.items)
这应该为下载了歌曲的物理设备返回一个非空数组。