8

从 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 中提取封面,而且是瞬间完成的,我不明白他们为什么要削弱这个功能。

4

2 回答 2

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];
于 2015-10-07T10:24:49.880 回答
0

I would recommend trying the new Swift implementation:

let query = MPMediaQuery.songsQuery()
let mediaCollection = MPMediaItemCollection(items: query.items!)
print(mediaCollection.items)

This should return a non-empty array for a physical device with songs downloaded.

于 2015-10-07T05:07:45.990 回答