自iOS 8以來,所有iPad專用藝術數據陣列& iPhone設備都會返回一個空數組。從本地文件(NSBundle)拉出時,我可以獲得專輯封面或封面藝術,但是從iTunes或設備上購買的任何歌曲都會返回空白。iOS 8專輯封面
我已經更新到最新的XCode,這兩個設備,並且還有在iTunes上最新的iOS。我已經在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中拉出封面,這是瞬間的,我不明白他們爲什麼會削弱這個功能。
你得到任何進一步這裏? – newenglander 2015-01-29 07:45:04
你有沒有試過MPMediaQuery.songsQuery()? – Aabglov 2015-10-07 01:43:28