当前曲目iOS Swift的音频信息 [英] Audio Information of Current Track iOS Swift
本文介绍了当前曲目iOS Swift的音频信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们说一首曲目正在播放(通过任何应用),我想获取歌曲信息,例如名称。当您锁定手机以及专辑封面时,它会显示在您的屏幕上。如何使用swift获取该信息?
Let's say a track is playing (through any app), and I want to get the song information, such as the name. It shows up on your screen when you lock the phone, along with the album cover. How can I get that information using swift?
推荐答案
以下是获取音频信息的示例函数:
Here is example function to get audio Info:
import MediaPlayer
var url:NSURL!
let audioInfo = MPNowPlayingInfoCenter.defaultCenter()
var nowPlayingInfo:[NSObject:AnyObject] = [:]
func fetchMP3Info() {
let playerItem = AVPlayerItem(URL: url) //this will be your audio source
println(url.path!)
let metadataList = playerItem.asset.metadata as! [AVMetadataItem]
for item in metadataList {
if item.commonKey != nil && item.value != nil {
if item.commonKey == "title" {
println(item.stringValue)
nowPlayingInfo[MPMediaItemPropertyTitle] = item.stringValue
}
if item.commonKey == "type" {
println(item.stringValue)
nowPlayingInfo[MPMediaItemPropertyGenre] = item.stringValue
}
if item.commonKey == "albumName" {
println(item.stringValue)
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = item.stringValue
}
if item.commonKey == "artist" {
println(item.stringValue)
nowPlayingInfo[MPMediaItemPropertyArtist] = item.stringValue
}
if item.commonKey == "artwork" {
if let image = UIImage(data: item.value as! NSData) {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: image)
println(image.description)
}
}
}
}
audioInfo.nowPlayingInfo = nowPlayingInfo
}
希望这会有所帮助。
这篇关于当前曲目iOS Swift的音频信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文