2015-05-13 28 views
4

我想知道是否有直接显示正在播放的音乐播放器正在播放的MP3文件的图像和标题的方式。我的代码如下,这个例子非常简单。我只想测试使用我包含在我的项目中的一个.MP3。使用Swift显示当前正在播放的标题.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. 
    } 


} 

到目前为止,我能够给println的完整路径的MP3文件,但一直没能为使用表示作为封面为我的MP3在项目中显示的图像或仅显示曲目播放的标题。有人可以帮我解决这个问题吗?我在任何地方都在Objective C中给出了一些例子,但是我不太熟悉那种语言来翻译Swift的代码。谢谢

+0

你可以添加一些代码或链接如何在Objective C中做到这一点?这里有很多可以很容易地将它转换为Swift。 – pteofil

+0

以下是解决其他语言问题的链接。我认为这样做更清洁,而不是试图在整个地方发布代码。 http://stackoverflow.com/questions/14030746/ios-avfoundation-how-do-i-fetch-artwork-from-an-mp3-file – Unconquered82

回答

9

您需要为媒体播放器添加导入语句并为nowPlayingInfo属性设置 General Media Item Property Keys 。对于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() 
} 
+0

谢谢。当我使用println(“nowPlayingInfo”)的时候,我使用了你的代码,结果是:[title:songName,artist:artistName]。我很困惑我如何使用你的代码来显示轨道图形和标题。我是否需要创建包含标题的变量并使用nowPlayingInfo代码调用它? – Unconquered82

+0

我能找到这个例子,但它不在Swift中。你能帮我翻译一下,所以我可以将它应用到我的Swift项目中吗? http://ios-blog.co.uk/tutorials/how-to-play-music-in-your-app/ – Unconquered82

+1

audioPath.lastPathComponent!.stringByDeletingPathExtension –

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 
      } 

     } 
    } 
} 

您可以选择选择哪个commonkeys想与使用,如果这样的语句,并打印文本到像这样的标签:

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

谢谢莱昂纳多的帮助!

+1

我编辑了我的答案AVMetadataItem已经有了一个stringValue属性,因此不需要将值转换为字符串 –

+0

https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVMetadataItem_Class/index.html #// apple_ref/OCC/instp/AVMetadataItem/stringValue的 –

相关问题