使用 Swift 显示当前播放的.MP3 的标题
Using Swift to display title of currently playing .MP3
我想知道是否有一种直接的方式来显示我的音乐播放器正在播放的当前播放的 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 在项目中的封面艺术或仅显示标题曲目播放。有人可以帮我解决这个问题吗?
您需要为媒体播放器添加导入语句并设置
General Media Item Property Keys
nowPlayingInfo property. For the MPMediaItemPropertyArtwork you need to take a look at this 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()
}
在 Leonardo 的帮助下,我能够使用以下代码完成此操作:
@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
}
感谢莱昂纳多的协助!
我想知道是否有一种直接的方式来显示我的音乐播放器正在播放的当前播放的 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 在项目中的封面艺术或仅显示标题曲目播放。有人可以帮我解决这个问题吗?
您需要为媒体播放器添加导入语句并设置 General Media Item Property Keys nowPlayingInfo property. For the MPMediaItemPropertyArtwork you need to take a look at this 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()
}
在 Leonardo 的帮助下,我能够使用以下代码完成此操作:
@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
}
感谢莱昂纳多的协助!