Search code examples
iosios5mpmediaitem

iOS - MPMediaItem Display a Default Artwork


I am currently developing an app that shows what artist, track and album art you're listening to in the Music player. All is going well apart from when I play a song with no artwork I want to be able to show my own default image (as opposed to showing a blank screen).

The below is how I imagined it SHOULD work however it never gets into the else as the itemArtwork is never nil!

You're help is appreciated.

Thanks, Ben

_item = [_player nowPlayingItem];
MPMediaItemArtwork *itemArtwork = [_item valueForProperty:MPMediaItemPropertyArtwork];

if (itemArtwork != nil) {
    UIImage *albumArtworkImage = [itemArtwork imageWithSize:CGSizeMake(250.0, 250.0)];
    _albumArtImageView.image = albumArtworkImage;
} else { // no album artwork
    NSLog(@"No ALBUM ARTWORK");
    _albumArtImageView.image = [UIImage imageNamed:@"kol.jpg"];
}

Solution

  • MPMediaItemArtwork seem to always exist, even for tracks that don't have artwork.

    The way I detect if there's no image is to see if MPMediaItemArtwork's imageWithSize returns NULL.

    Or, rejiggering your code a bit:

    _item = [_player nowPlayingItem];
    UIImage *albumArtworkImage = NULL;
    MPMediaItemArtwork *itemArtwork = [_item valueForProperty:MPMediaItemPropertyArtwork];
    
    if (itemArtwork != nil) {
        albumArtworkImage = [itemArtwork imageWithSize:CGSizeMake(250.0, 250.0)];
    }
    
    if (albumArtworkImage) {
        _albumArtImageView.image = albumArtworkImage;
    } else { // no album artwork
        NSLog(@"No ALBUM ARTWORK");
        _albumArtImageView.image = [UIImage imageNamed:@"kol.jpg"];
    }
    

    I hope this info helps you out (and if so, mark this answer as checked :-)