Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"];
}
like image 976
Ben Hutchinson Avatar asked Oct 19 '11 16:10

Ben Hutchinson


2 Answers

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 :-)

like image 71
Michael Dautermann Avatar answered Oct 30 '22 11:10

Michael Dautermann


If you just need to check if the artwork exists or not (without possibly grabbing the image, which burns a lot of CPU cycles) you can also check the itemArtwork.bounds property. If the artwork does not exist, the bounds.size.width and bounds.size.height properties will be 0:

MPMediaItemArtwork *artwork = [_item valueForProperty:MPMediaItemPropertyArtwork];
BOOL hasArtwork = (artwork.bounds.size.width > 0 && artwork.bounds.size.height > 0);
like image 20
Peter Sobot Avatar answered Oct 30 '22 09:10

Peter Sobot