如何使用swift获取当前播放音乐的专辑封面? [英] How do I get the album artwork of the currently playing music using swift?

查看:271
本文介绍了如何使用swift获取当前播放音乐的专辑封面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用当前在音乐播放器上播放的歌曲的专辑插图创建UIImageView。我找到的所有资源都是在objective-c(我不知道)或不工作(也许我没有正确实现)。

I am trying to create a UIImageView with the album artwork of the song currently playing on a music player. All of the resources I have found are either in objective-c (which I don't know) or do not work (maybe I am not implementing it correctly).

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

I am currently trying to do this with this code:

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()


推荐答案

试试这个:

//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))
    }

这篇关于如何使用swift获取当前播放音乐的专辑封面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆