Unable to update the MPMediaItem (Lock Screen Media Controls)

I'm currently developing an app that can reproduce audio and video from the web (my own server) using SwiftUI targeting iOS15 and testing on a iPhone XS Pro Max running iOS16.2 (real device)

Both the video and audio play just fine but I get this (image in the end of the post) in as the media controls. The current time and duration are correct and updated (without me calling to update them) but I'm unable to actually update the title, artist, album and artwork

My first thought was to update the NowPlaying and after a lengthy search I'm yet to find a solution that works

I created a small class to update the NowPlaying info

import Foundation
import UIKit
import MediaPlayer

public class MediaController {

    var nowPlayingInfo = [String: Any]()
    static let shared = MediaController()

    private init() { }

    func setupNotificationView(title: String?, album: String?, artist: String?, thumbnailUrl: String) {

        nowPlayingInfo = [String : Any]()
        nowPlayingInfo[MPMediaItemPropertyTitle] = title
        nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = album
        nowPlayingInfo[MPMediaItemPropertyArtist] = artist
        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo

    }
}

The NowPlaying still doesn't update

This is the call sequence when I press play

let avAsset = AVURLAsset(url: URL(string: "https://domain.com/video.mp4")

let avItem = AVPlayerItem(asset: avAsset)
player.replaceCurrentItem(with: avItem)

do {
    try AVAudioSession.sharedInstance().setActive(true);
} catch { print(error) }
player.play()
MediaController.shared.setupNotificationView(title: "A Title", album: "A Album", artist: "A Artist", thumbnailUrl: "")

Although the app is being developed using SwiftUI I'm using the UIViewControllerRepresentable to display a the video using AVPlayerViewController since the current one misses the PiP

and under makeUIViewController I have the following code

let controller = AVPlayerViewController()
controller.player = player
controller.allowsPictureInPicturePlayback = true
controller.entersFullScreenWhenPlaybackBegins = true
controller.delegate = context.coordinator

and on the app launch I have the following

let audioSession = AVAudioSession.sharedInstance()

do {
    try audioSession.setCategory(AVAudioSession.Category.playback)
} catch {
    print("Setting category to AVAudioSessionCategoryPlayback failed.")
}

And yes I have the background mode capability enabled for "Audio, AirPlay and Picture in Picture"

I'm at a lost here and hope someone can help.

Answered by PedroCavaleiro in 740879022

I got the answer. As soon as I registered for remote controls the MPMediaInfo appeared correctly

I discovered that what was causing the MPNowPlayingInfoCenter to be update was the AVPlayerViewController (as I believed) setting controller.updatesNowPlayingInfoCenter = false prevented this unwanted update

Now a new issue appears, the MPNowPlayingInfoCenter doesn't appear at all

On the init() I have this line to monitor the playback status

player.publisher(for: \.timeControlStatus)
            .sink { [weak self] status in
                switch status {
                case .playing:
                    self?.isPlaying = true
                    try? AVAudioSession.sharedInstance().setActive(true)
                    self?.setupNowPlaying()
                case .paused:
                    self?.isPlaying = false
                case .waitingToPlayAtSpecifiedRate:
                    break
                @unknown default:
                    break
                }
            }
            .store(in: &subscriptions)

Here's the function that updates (or at least should) update the MPNowPlayingInfo

func setupNowPlaying() {

        var nowPlayingInfo = [String: Any]()

        nowPlayingInfo[MPNowPlayingInfoPropertyMediaType] = MPNowPlayingInfoMediaType.audio.rawValue
        nowPlayingInfo[MPNowPlayingInfoPropertyIsLiveStream] = false
        nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
        nowPlayingInfo[MPMediaItemPropertyTitle] = "Track Title"
        nowPlayingInfo[MPMediaItemPropertyArtist] = "Track Artist"
        nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration
        nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTime
        

        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo    

    }

I feel that I'm close to the answer but still out of reach

Accepted Answer

I got the answer. As soon as I registered for remote controls the MPMediaInfo appeared correctly

Unable to update the MPMediaItem (Lock Screen Media Controls)
 
 
Q