iOS - MPMediaItem Display a Default Artwork
Asked Answered
S

2

17

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"];
}
Slat answered 19/10, 2011 at 16:57 Comment(0)
E
34

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

Eugine answered 19/10, 2011 at 18:0 Comment(1)
Thanks Michael, exactly what I needed.. all so simple now! It'd be much easier if everyone kept their iTunes library pretty like me though! Thanks again, BenSlat
R
3

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);
Residuary answered 17/11, 2013 at 15:45 Comment(1)
Nice, will certainly give this a try next time!Slat

© 2022 - 2024 — McMap. All rights reserved.