使用Swift显示当前正在播放的标题.MP3(Using Swift to display title of currently playing .MP3)

编程入门 行业动态 更新时间:2024-10-10 15:24:06
使用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封面图片的图像,或者只显示播放曲目的标题。 有人可以帮我解决这个问题吗? 我在任何地方都在Objective C中给出了一些例子,但是我不太熟悉那种语言来翻译Swift的代码。 谢谢

I was wondering if there was a straight forward way of displaying the image and title of the currently playing MP3 file my music player is playing. My code is as follows and is very simple for this example. I am only wanting to test using one .MP3 that I've included in my project.

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. } }

So far I am able to println the full path to the MP3 file but have not been able to use the display the image that is showing as the cover art for my MP3 in the project or to display only the title of the track playing. Could someone help me out with this?

最满意答案

您需要添加媒体播放器的导入语句并为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() }

you need to add the import statement for Media player and set the General Media Item Property Keys for 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"]

To extract the metadata from your mp3 you need to create an AVPlayerItem and access its asset commonMetadata property

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

Note: You will have to invoke beginReceivingRemoteControlEvents() otherwise it will not work on the actual device.

override func viewDidLoad() { super.viewDidLoad() UIApplication.sharedApplication().beginReceivingRemoteControlEvents() }

更多推荐

本文发布于:2023-08-07 18:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正在播放   标题   Swift   playing   title

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!