我在设置MPNowPlayingInfoCenter的currentTime和songLength时遇到问题。这是我的代码:
func updateNowPlayingCenter (title: String, artist: String, albumArt: AnyObject, currentTime: NSNumber, songLength: NSNumber, PlaybackRate: Double){
var songInfo: Dictionary <NSObject, AnyObject> = [
MPMediaItemPropertyTitle as NSObject: title as AnyObject,
MPMediaItemPropertyArtist as NSObject: artist as AnyObject,
MPMediaItemPropertyArtwork as NSObject: ???,
MPNowPlayingInfoPropertyElapsedPlaybackTime as NSObject: currentTime,
MPMediaItemPropertyPlaybackDuration as NSObject: songLength,
MPNowPlayingInfoPropertyPlaybackRate as NSObject: PlaybackRate as AnyObject
]
MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo as [NSObject : AnyObject] as! [String : Any]
}
这是我在viewWillAppear中设置属性的地方:
updateNowPlayingCenter(title: titleText[thisSong], artist: authorText[thisSong], albumArt: ??? as AnyObject, currentTime: ???, songLength: ???, PlaybackRate: 1.0)
我尝试使用audioPlayer.currentTime和audioPlayer.duration,但它没有用。我该怎么做呢?另外,我无法弄清楚如何设置MPMediaItemPropertyArtwork的图像。我有我的assests中的文件,图像名称等于titleText [thisSong]。如果你也能帮助我,那就太棒了!谢谢!
答案 0 :(得分:0)
Apple的Media Playback Programming Guide提供了答案:
func setupNowPlaying() {
// Define Now Playing Info
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = "My Movie"
if let image = UIImage(named: "lockscreen") {
nowPlayingInfo[MPMediaItemPropertyArtwork] =
MPMediaItemArtwork(boundsSize: image.size) { size in
return image
}
}
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playerItem.currentTime().seconds
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = playerItem.asset.duration.seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
// Set the metadata
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}