我有两种方法可以在CC/Lock screen中显示媒体信息
func updateGeneralMetadata() {
guard player.url != nil, let _ = player.url else {
nowPlayingInfoCenter.nowPlayingInfo = nil
return
}
let item = currentItem
var rating = ""
for _ in 0 ..< item!.rating{
rating.append("*")
}
if item?.assetURL != nil{
MPNowPlayingInfoCenter.default().nowPlayingInfo = [
MPMediaItemPropertyTitle: item?.value(forProperty: MPMediaItemPropertyTitle)!,
MPMediaItemPropertyArtist: item?.value(forProperty: MPMediaItemPropertyArtist)!,
MPMediaItemPropertyAlbumTitle: rating,
MPMediaItemPropertyArtwork: item?.artwork ?? UIImage()
]
}
}
func updatePlaybackRateData(){
guard currentItem?.assetURL != nil else {
duration = 0
nowPlayingInfoCenter.nowPlayingInfo = nil
return
}
duration = Float(player.duration)
let item = currentItem
MPNowPlayingInfoCenter.default().nowPlayingInfo = [
MPNowPlayingInfoPropertyElapsedPlaybackTime: player.currentTime,
MPNowPlayingInfoPropertyPlaybackRate: player.rate,
MPMediaItemPropertyPlaybackDuration: item?.playbackDuration
]
}和播放媒体的功能
func play(){
player.play()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(Plum.updatePlaybackRateData), userInfo: nil, repeats: true)
timer.fire()
NotificationCenter.default.post(name: Plum.playBackStateChanged, object: nil, userInfo: ["Artist": "Title"])
updateGeneralMetadata()
}如你所见,我希望每秒更新一次播放速率,并且仅在媒体文件发生更改时更新一般元数据。当我有像那样的两个函数时,似乎只有updateGeneralData()有效,因为当我把所有的信息,如标题,艺术家,albumTitle,艺术作品,currentTime,速率和持续时间都显示在updatePlaybackRateData()中时,LS/CC上没有回放时间条,但我想在两个方法之间拆分这种功能性,以便只有必要的信息每秒更新一次。
发布于 2017-10-10 19:15:38
显然,下面的解决方案解决了这个问题。我创建了一个新的常量
let infoCC = MPNowPlayingInfoCenter.default()
并将方法更改为:
func updateGeneralMetadata() {
guard player.url != nil, let _ = player.url else {
infoCC.nowPlayingInfo = nil
return
}
let item = currentItem
var nowPlayingInfo = infoCC.nowPlayingInfo ?? [String: Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = item?.title
nowPlayingInfo[MPMediaItemPropertyArtist] = item?.albumArtist
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = item?.albumTitle
nowPlayingInfo[MPMediaItemPropertyArtwork] = item?.artwork
infoCC.nowPlayingInfo = nowPlayingInfo
}
func updatePlaybackRateData(){
guard currentItem?.assetURL != nil else {
duration = 0
infoCC.nowPlayingInfo = nil
return
}
var nowPlayingInfo = infoCC.nowPlayingInfo ?? [String: Any]()
duration = Float(player.duration)
let item = currentItem
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = player.duration
infoCC.nowPlayingInfo = nowPlayingInfo
}希望它能在未来帮助某些人
https://stackoverflow.com/questions/46663944
复制相似问题