0

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.

Adam
  • 1,776
  • 1
  • 17
  • 28

1 Answers1

0

Apparently the solution below solves the problem. I've created new constant

let infoCC = MPNowPlayingInfoCenter.default()

and changed the methods to:

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
}

Hopefully it helps someone in the future

Adam
  • 1,776
  • 1
  • 17
  • 28