How to display album art streaming Apple Music?

In my music player using the new Apple Music API (released May 12, 2016) in iOS 9 (quickly, although I am still familiar with Objective-C), I can display information from a streaming song, but not a work of art. I use MediaPlayer, UIKit, and StoreKit environments. I have successfully requested permission for AddToCloudMusicLibrary and MusicCatalogPlayback. I have success showing the work of Apple Music from songs that I downloaded from the Apple Music app, as well as illustrations from my personal song collection. I saw other people with problems about this, with no luck ...

Just try to ask again, for people, before starting to display the default image (which is necessary in any case for error handling) or pull it out of an alternative service. Any help would be great! This is actually not an error in my code, so I won’t show it unless I’m asked to help solve this problem.

My first attempt at publishing code ... Here's what I have in a quick file called "Authorization". Do I need to reference this code somewhere or should it be in the AppDelegate file? This is the only part of my project on which I am not 100% sure.

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()
                    }
                }
            }
        }
    }
}
+4
source share
3 answers

API Apple Music. , , , AM, .

bugreport.apple.com( 6 25413082).

iTunes api , , 100% ...

+3

. - , , UIImageView nowPlayingItem , . Apple Music api. . . , Apple . , .

func updateUserInterface() {
    if musicPlayerManager.musicPlayerController.playbackState == .playing { //Check if anything is playing
        if let currentItem = musicPlayerManager.musicPlayerController.nowPlayingItem {
            songAlbumLabel.text = currentItem.albumTitle
            songTitleLabel.text = currentItem.title
            songArtistNameLabel.text = currentItem.artist
            let playbackStoreID = currentItem.playbackStoreID
            if let artwork = musicPlayerManager.musicPlayerController.nowPlayingItem?.artwork, let image = artwork.image(at: artworkImageView.frame.size) { // Check for local image
                print("using local image")
                artworkImageView.image = image 
        } else { 
            guard let developerToken = appleMusicManager.fetchDeveloperToken() else {print("oops");return}
            let searchTypes = "songs"
            var searchURLComponents = URLComponents()
            searchURLComponents.scheme = "https"
            searchURLComponents.host = "api.music.apple.com"
            searchURLComponents.path = "/v1/catalog/"
            searchURLComponents.path += "\(authorizationManager.cloudServiceStoreFrontCountryCode)"
            searchURLComponents.path += "/search"
            searchURLComponents.queryItems = [
                URLQueryItem(name: "term", value: playbackStoreID),
                URLQueryItem(name: "types", value: searchTypes)
            ]
            var request = URLRequest(url: searchURLComponents.url!)
            request.httpMethod = "GET"
            request.addValue("Bearer \(developerToken)", forHTTPHeaderField: "Authorization")
            let dataTask = URLSession.shared.dataTask(with: request) {
                (data, response, error) in
                print(response!)
                if let searchData = data {
                    guard let results = try? self.appleMusicManager.processMediaItemSections(searchData) else { return}
                    self.mediaItem = results
                    let album = self.mediaItem[0][0]
                    let albumArtworkURL = album.artwork.imageUrl(self.artworkImageView.frame.size)
                    if let image = self.imageManager.cachedImage(url: albumArtworkURL) {
                        print("using cached image")
                        self.artworkImageView.image = image
                    }
                    else {
                        self.imageManager.fetchImage(url: albumArtworkURL) {(image) in
                            print("fetching image")
                            self.artworkImageView.image = image
                        }
                    }
                }
            }
            dataTask.resume()
        }
    } else { // Need to set it to some kind of default values
        artworkImageView.image = nil
        songArtistNameLabel.text = " "
        songTitleLabel.text = " "
        songAlbumLabel.text = " "
        }
    }
}
+1

MediaItem MPMediaItemPropertyArtwork. :

MPMediaItemArtwork *artwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];
UIImage *albumImage = [itemArtwork imageWithSize:cgSizeYouWant];
0

Source: https://habr.com/ru/post/1648116/


All Articles