I have two methods that are supposed to display media info in 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
]
}
And the function for playing media
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()
}
As you can see I want to update Playback rate every second and general metadata only when a media file is changed. When I have both functions like that, it seems that only updateGeneralData() works because there is no playback time bar on LS/CC When I put all the info, like title, artist, albumTitle, artwork, currentTime, rate and duration in updatePlaybackRateData() everything shows up, but I want to split that funcionality between two methods so only the necessary info gets updated every second.