6

我想知道是否有一种直接的方式来显示我的音乐播放器正在播放的当前播放的 MP3 文件的图像和标题。我的代码如下,对于这个例子来说非常简单。我只想使用我项目中包含的一个 .MP3 进行测试。

class ViewController: UIViewController {
    let audioPath:NSURL! = NSBundle.mainBundle().URLForResource("SippinOnFire", withExtension: "mp3")

    @IBOutlet var sliderValue: UISlider!
    var player:AVAudioPlayer = AVAudioPlayer()


    @IBAction func play(sender: AnyObject) {

        player.play()
        //println("Playing \(audioPath)")
    }
    @IBAction func pause(sender: AnyObject) {

        player.pause()
    }
    @IBAction func stop(sender: AnyObject) {

        player.stop()
        player.currentTime = 0;
    }
    @IBAction func sliderChanged(sender: AnyObject) {

        player.volume = sliderValue.value

    }

    override func viewDidLoad() {
        super.viewDidLoad()

                 var error:NSError? = nil

        player = AVAudioPlayer(contentsOfURL: audioPath!, error: &error)

        player.volume = 0.5;

    }

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


}

到目前为止,我能够打印 MP3 文件的完整路径,但无法在项目中使用显示为我的 MP3 封面的图像或仅显示正在播放的曲目的标题。有人可以帮我解决这个问题吗?

4

2 回答 2

11

您需要为媒体播放器添加导入语句,并 为nowPlayingInfo属性设置通用媒体项属性键。对于 MPMediaItemPropertyArtwork,您需要查看此MPMediaItemArtwork

import MediaPlayer

let audioInfo = MPNowPlayingInfoCenter.defaultCenter()
let audioName = audioPath.lastPathComponent!.stringByDeletingPathExtension
audioInfo.nowPlayingInfo = [ MPMediaItemPropertyTitle: audioName, MPMediaItemPropertyArtist:"artistName"]

要从您的 mp3 中提取元数据,您需要创建一个 AVPlayerItem 并访问其资产 commonMetadata 属性

let playerItem = AVPlayerItem(URL: audioPath)
let metadataList = playerItem.asset.commonMetadata as! [AVMetadataItem]
for item in metadataList {
    if item.commonKey == "title" {
        println("Title = " + item.stringValue)
    }
    if item.commonKey == "artist" {
        println("Artist = " + item.stringValue)
    }
}       

注意:您必须调用 beginReceivingRemoteControlEvents() 否则它将无法在实际设备上运行。

override func viewDidLoad() {
    super.viewDidLoad()
    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
}
于 2015-05-13T13:31:01.027 回答
1

在莱昂纳多的帮助下,我能够使用以下代码完成此操作:

@IBAction func play(sender: AnyObject) {


    let audioInfo = MPNowPlayingInfoCenter.defaultCenter()
println(audioInfo)


    player.play()
    //println("Playing \(audioPath)")


    let playerItem = AVPlayerItem(URL: audioPath)
    let metadataList = playerItem.asset.commonMetadata as! [AVMetadataItem]


    for item in metadataList {
        if let stringValue = item.value as? String {
           println(item.commonKey)
            if item.commonKey == "title" {
                trackLabel.text = stringValue
            }
            if item.commonKey == "artist" {
                artistLabel.text = stringValue
            }

        }
    }
}

您可以有选择地选择要与这些 if 语句一起使用的公共键,并将文本打印到标签上,如下所示:

            if item.commonKey == "title" {
                trackLabel.text = stringValue
            }
            if item.commonKey == "artist" {
                artistLabel.text = stringValue
            }

谢谢莱昂纳多的帮助!

于 2015-05-14T17:43:47.937 回答