从iOS 8开始,所有来自iPad和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中提取封面艺术并且它是瞬间的,我不明白为什么他们会对这种功能产生影响。
答案 0 :(得分:1)
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];
答案 1 :(得分:0)
我建议尝试新的Swift实现:
let query = MPMediaQuery.songsQuery()
let mediaCollection = MPMediaItemCollection(items: query.items!)
print(mediaCollection.items)
这应该为已下载歌曲的物理设备返回非空数组。