小编典典

如何使用swift获取当前正在播放的音乐的专辑插图?

swift

我正在尝试使用当前在音乐播放器上播放的歌曲的专辑插图创建UIImageView。我发现的所有资源要么在Objective-
C中(我不知道),要么不起作用(也许我没有正确实现)。

我目前正在尝试使用以下代码执行此操作:

class ViewController: UIViewController {

@IBOutlet weak var backgroundAlbum: UIImageView!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    backgroundAlbum.image = MPMediaItemArtwork()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

但我在一行中遇到错误

backgroundAlbum.image = MPMediaItemArtwork()

阅读 229

收藏
2020-07-07

共1个答案

小编典典

试试这个:

//this method is called after you picked a song from music library
func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {

    dismissViewControllerAnimated(true, completion: nil)

    //get current song
    let currentSong: MPMediaItem = mediaItemCollection.items[0]

    if let artwork: MPMediaItemArtwork = currentSong.valueForProperty(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork{
        albumArt.image = artwork.imageWithSize(CGSize(width: x, height: y))
    }
2020-07-07