1

When trying to grab and display song covers/artworks with MPMediaItemPropertyArtwork, they fail to display in my UIImageView. Additionally, with the other MPMediaItems showing the Artist Name and Song Name, they do not update when the song is changed.

This is my .h file:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *songtitle;
@property (weak, nonatomic) IBOutlet UILabel *songartist;
@property (weak, nonatomic) IBOutlet UILabel *nowplaying;
@property (weak, nonatomic) IBOutlet UIImageView *songalbumart;


@end

This is part of my .m file that has the MPMusicPlayerController content:

MPMusicPlayerController *player = [MPMusicPlayerController systemMusicPlayer];

MPMediaItem *item = [player nowPlayingItem];

NSString *songtitlestring = [item valueForProperty:MPMediaItemPropertyTitle];
NSString *songartiststring = [item valueForProperty:MPMediaItemPropertyArtist];
NSString *nowplayingstring = [NSString stringWithFormat:@"#NowPlaying %@ by %@, via @TweetPlaying", songtitlestring, songartiststring];

self.songalbumart = [item valueForProperty:MPMediaItemPropertyArtwork];

self.songtitle.text = songtitlestring;
self.songartist.text = songartiststring;
self.nowplaying.text = nowplayingstring;

Anyone know how to resolve these issues of:

  • MPMediaItemPropertyArtwork not displaying (IBOutlet is connected!)
  • MPMediaItems of the UILabels to display Artist Name and Song Name not updating when song is changed by user

I am developing for iOS 8 and am there using MPMusicPlayerController's systemMusicPlayer as iPodMusicPlayer is depreciated.

Any help would be appreciated!

Edit: So I have to use MPMusicPlayerControllerPlaybackStateDidChangeNotification, can anyone provide an example for my situation?

iPwnTech
  • 545
  • 2
  • 5
  • 20
  • `[self.songalbumart setImage:[[item valueForProperty:MPMediaItemPropertyArtwork] imageWithSize:CGSizeMake(songalbumart.frame.size.width, songalbumart.frame.size.height)];` ? You say that info are not updating, but do you listen when the music change? – Larme Oct 02 '14 at 13:39
  • @Larme I am using an IBOutlet to link the MKMediaPropertyArtwork to a UIImageView on my Storyboard though, will that code still work? And no, the application isn't listening when the music changes, would you be able to provide a solution for that possibly? – iPwnTech Oct 02 '14 at 13:42
  • I think that you still may be able to use ` MPMusicPlayerControllerNowPlayingItemDidChangeNotification` There should be some example on the web. – Larme Oct 02 '14 at 13:47
  • @Larme Can you provide something I could like at? Can't find anything quite relevant to my situation. – iPwnTech Oct 02 '14 at 13:57
  • Look for example using `iPodMusicPlayer`, and replace it by `systemMusicPlayer`. http://stackoverflow.com/questions/25788671/detect-when-music-player-is-being-paused/25788824#25788824 (and change the notification that you listen but you'll get how it works). – Larme Oct 02 '14 at 14:10
  • @Larme Just tried `[self.songalbumart setImage:[[item valueForProperty:MPMediaItemPropertyArtwork] imageWithSize:CGSizeMake(songalbumart.frame.size.width, songalbumart.frame.size.height)];`, doesn't work, any ideas? – iPwnTech Oct 02 '14 at 15:00
  • Is `[item valueForProperty:MPMediaItemPropertyArtwork]` nil? – Larme Oct 02 '14 at 15:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62348/discussion-between-rastudios-and-larme). – iPwnTech Oct 02 '14 at 15:08
  • Possible duplicate of [MPMediaItemArtwork is null while cover is available in iTunes](http://stackoverflow.com/questions/25998621/mpmediaitemartwork-is-null-while-cover-is-available-in-itunes) – Ric Santos Mar 02 '16 at 09:27

1 Answers1

0

Following code worked for me.

MPMediaItemArtwork *artwork = [mediaItem valueForProperty: MPMediaItemPropertyArtwork];
UIImage *artworkImage = [artwork imageWithSize: albumImageView.bounds.size];

if (artworkImage) {
    albumImageView.image = artworkImage;
} else {
    albumImageView.image = [UIImage imageNamed: @"noArtwork.png"];
}

Got answer from this link

Viral Narshana
  • 1,855
  • 2
  • 21
  • 45