开发者

AVFoundation get album image of song

开发者 https://www.devze.com 2023-04-02 04:50 出处:网络
I wan开发者_运维技巧t to get album image/cover of song. Is it possible? I\'m using AVPlayer for audio streaming, and i need to get album cover, if song contains it. Like in iTunes.

I wan开发者_运维技巧t to get album image/cover of song. Is it possible? I'm using AVPlayer for audio streaming, and i need to get album cover, if song contains it. Like in iTunes.

SOLUTION:

UIImage *img = nil;
NSArray *metadata = _avplayer.currentItem.asset.commonMetadata;
for(AVMetadataItem *item in metadata){
    if([item.commonKey isEqualToString:@"artwork"]){
       NSData *data = item.value[@"data"];
       img = [UIImage imageWithData:data];
       return img;
    }
}
return img;


You should try this, but I use MPMusicPlayer:

musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
currentItem = [musicPlayer nowPlayingItem];
if (currentItem == nil) {
    //Not playing
}
else 
{
    NSString *name = [currentItem valueForProperty:@"title"];
    int length = [name length];
    if (length > 30) {
        name = [name substringWithRange:NSMakeRange(0, 30)];
        name = [name stringByAppendingString:@"..."];
    }
    NSString *album = [currentItem valueForProperty:@"albumTitle"];
    int lengthAl = [album length];
    if (lengthAl > 30) {
        album = [album substringWithRange:NSMakeRange(0, 30)];
        album = [album stringByAppendingString:@"..."];
    }
    NSString *artist = [currentItem valueForProperty:@"artist"];
    int lengthAr = [artist length];
    if (lengthAr > 30) {
        artist = [artist substringWithRange:NSMakeRange(0, 30)];
        artist = [artist stringByAppendingString:@"..."];
    }
    MPMediaItemArtwork *artworkPrev = [currentItem valueForProperty:@"artwork"];
    if (artworkPrev != nil)
    {
        UIImage *img = [artworkPrev imageWithSize:CGSizeMake(65, 65)];
        UIImageView *imgNew = [[UIImageView alloc] initWithImage:img];
        imgNew.frame = CGRectMake(-15,-20, 65, 65);
    }
}

As you can see I use a NSMakeRange, I use it only to not leave the space in my application. I hope that helps you


Provided your files are the right format, i.e. MP3, you can load the file's data into an ID3 tag parser. Here's an example of one. It's made for OS X, but it looks like it should port to iOS fairly easily.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号