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"];
}
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 :-)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With