在我的音乐播放器中使用新的Apple Music API(2016年5月12日发布)在iOS 9(swift,尽管我目前也熟悉Objective-C)中,我可以显示流媒体歌曲的信息,但不能显示插图。我正在使用MediaPlayer,UIKit和StoreKit框架。我已经成功地请求了AddToCloudMusicLibrary和MusicCatalogPlayback的授权。我成功地展示了从Apple Music应用程序下载的歌曲中的Apple Music插图,以及我个人歌曲收藏中的插图。我见过其他人在这方面有问题,但没有运气......
只是在我诉诸于显示默认图像(无论如何对于错误处理都是必要的)或从备用服务中提取之前,尝试再问一次,为人们。任何帮助都会很棒!在我的代码中不是真正的错误,所以我不会显示它,除非要求帮助解决这个问题。
我第一次尝试发布代码...这是我在一个名为授权的快速文件中的内容。我是否需要在任何地方引用此代码,还是应该将其放在 AppDelegate 文件中?这是我项目中唯一不是100%确定的部分。
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()
}
}
}
}
}
}
这是当前 Apple Music API 中的一个错误。正如您提到的,不在用户库中而是从 AM 流式传输的歌曲将不会返回专辑插图。
您应该在 bugreport.apple.com 提交功能增强(您也可以从 6 个月前25413082欺骗我的)。
与此同时,您最好的选择是使用 iTunes api 检索专辑插图,不幸的是,这不会 100% 返回正确的结果......
我为此挣扎了很长一段时间。如果这是一个实际的错误,我不确定,但是您无法像预期的那样为nowPlayingItem
设置UIImageView
。您必须从 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 albumTitle = currentItem.albumTitle //Changes here!
let artist = currentItem.artist //Changes here!
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"
let expectedArtist = artist?.replacingOccurrences(of: " ", with: "+") //Changes here!
let artistExpected = expectedArtist?.replacingOccurrences(of: "&", with: "") //Changes here!
let expectingArtist = artistExpected?.replacingOccurrences(of: "++", with: "+") //Changes here!
let expectedAlbum = albumTitle?.replacingOccurrences(of: " ", with: "+") //Changes here!
searchURLComponents.queryItems = [
URLQueryItem(name: "term", value: (expectingArtist! + "-" + expectedAlbum!)),
URLQueryItem(name: "types", value: searchTypes)
] //Changes here!
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 = " "
}
}
}
在 MediaItem 类中,有一个名为 MPMediaItemPropertyArtwork 的属性。您将能够通过以下方式检索相册的插图:
MPMediaItemArtwork *artwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];
UIImage *albumImage = [itemArtwork imageWithSize:cgSizeYouWant];
我发现的最佳方法是使用从目录 Apple Music API 请求获取歌曲,此处记录。
您只需要传入歌曲标识符(用于播放的标识符)和用于播放歌曲的店面标识符。与使用可能失败的搜索请求的 anwer 相比,这个 sould 总是产生准确的结果。
有了 SwiftUI,它变得超级简单——
if let artwork = mediaItem.artwork {
ArtworkImage(artwork, width: 85) // or whatever width you need.
.cornerRadius(10)
}