Get album artwork from MP3 file/ID3 tag

2019-02-14 14:42发布

I'm trying to get the album artwork from a MP3 file.

In this case I use AVAudioPlayer to play the file.

Here's the code that I thought would get the album artwork:

MPMusicPlayerController *controller = [MPMusicPlayerController applicationMusicPlayer];

MPMediaItem *current = controller.nowPlayingItem;

MPMediaItemArtwork *artwork = [current valueForProperty:MPMediaItemPropertyArtwork];
UIImage *artwork2 = [artwork imageWithSize:artwork.bounds.size];
[artworkView setImage:artwork2];

However artworkView doesn't contain any image whatsoever.

I'm a little bit stuck.

If any one could help by providing suggestions to where/how I can get the artwork directly from the ID3 tag, that would be of great use.

Any help appreciated.

1条回答
2楼-- · 2019-02-14 15:03

You have to use enumerateValuesForProperties here is a sample:

[item enumerateValuesForProperties:[NSSet setWithObjects:MPMediaItemPropertyTitle,MPMediaItemPropertyAlbumTitle,MPMediaItemPropertyArtist,MPMediaItemPropertyArtwork,nil]
                                 usingBlock:^(NSString *property, id value, BOOL *stop) {
                                     if ([property isEqualToString:MPMediaItemPropertyTitle]){
                                         if (value){
                                             titre=value;
                                         }else {
                                             titre=@"";
                                         }
                                     }
                                     if ([property isEqualToString:MPMediaItemPropertyArtist]){
                                         if(value){
                                             artist=value;
                                         }else {
                                             artist=@"";
                                         }

                                     }
                                     if ([property isEqualToString:MPMediaItemPropertyArtwork]){
                                         MPMediaItemArtwork *art=value;
                                         if (art!=nil){
                                             imgV.image=[art imageWithSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.width)];
                                     }

                                     }


                                 }];
查看更多
登录 后发表回答