Search code examples
iosobjective-cswiftmedia-playerapple-music

How to display album artwork of streamed Apple Music?


In my music player using the new Apple Music API(released May 12, 2016) in iOS 9 (swift, although I'm currently getting familiar with Objective-C as well), I can display information from a streamed song, but not the artwork. I am using MediaPlayer, UIKit, and StoreKit frameworks. I have been successful requesting authorization to AddToCloudMusicLibrary and MusicCatalogPlayback. I have success displaying Apple Music artwork from songs that I downloaded from the Apple Music app, as well as artwork from my personal song collection. I have seen other people with issues on this, with no luck...

Just going to try asking one more time, for the people, before I resort to displaying a default image(which is necessary anyway for error-handling), or pull from an alternate service. Any help would be great! Not really an error in my code so I won't show it, unless requested to help solve this problem.

My first attempt at posting code...Here is what I have in a swift file called Authorization. Do I need to reference this code anywhere or should it be in the AppDelegate file instead? This is the only part of my project I am not 100% sure of.

import StoreKit
import MediaPlayer
import UIKit

class AppleMusicPlayer: NSObject {

    let applePlayer = MPMusicPlayerController.systemMusicPlayer()

    func playID(productID: String) {
        SKCloudServiceController.requestAuthorization { status in
            let controller = SKCloudServiceController()
            controller.requestCapabilitiesWithCompletionHandler { capabilities, error in
                if capabilities != SKCloudServiceCapability.None {
                    MPMediaLibrary.defaultMediaLibrary().addItemWithProductID(productID) { entities, error in
                        self.appPlayer.setQueueWithStoreIDs([productID])
                        self.appPlayer.shuffleMode = .Songs
                        self.appPlayer.play()
                    }
                }
            }
        }
    }
}

Solution

  • This is a bug in the current API for Apple Music. As you mentioned, songs that are not in the user's library but are instead streamed from AM will not return album artwork.

    You should file a feature enhancement at bugreport.apple.com (you can also dupe mine from 6 months ago 25413082 ).

    In the meantime your best bet would be to use the iTunes api to retrieve the album artwork, unfortunately this doesn't return the correct results 100% of the time...