我有两种方法可以在CC / Lock屏幕上显示媒体信息
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()有效,因为LS / CC上没有播放时间条 当我在updatePlaybackRateData()中放置所有信息(如title,artist,albumTitle,artwork,currentTime,rate和duration)时,一切都会显示出来,但我想在两种方法之间分割这些功能,以便每秒只更新必要的信息。
答案 0 :(得分:0)
显然,下面的解决方案可以解决问题。我已经创建了新常量
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
}
希望将来可以帮助某人