当前音轨的iOSaudio信息Swift

比方说一个曲目正在播放(通过任何应用程序),我想获得歌曲的信息,如名称。 当您locking手机以及相册封面时,它会显示在屏幕上。 我如何使用swift获取这些信息?

以下是获取audio的示例函数信息:

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 } 

希望这会有所帮助。