作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在设置 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 的图像。我的 Assets 中有这些文件,图像名称等于 titleText[thisSong]。如果你也能帮助我,那就太好了!谢谢!
最佳答案
苹果的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
}
关于swift - 从 AVAudioPlayer 设置 MPNowPlayingInfoCenter currentTime 和 SongLength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44344851/
我在设置 MPNowPlayingInfoCenter 的 currentTime 和 SongLength 时遇到问题。这是我的代码: func updateNowPlayingCenter (ti
我是一名优秀的程序员,十分优秀!