iosswiftavplayermpremotecommandcenter

MPRemoteCommandCenter Not displaying in lock screen


May be it's a duplicate question, but none of the solutions working for me. I have tried out almost everything.

The now playing info is not getting updated in lock screen.

Swift Version : 5 & iOS Version : 13

Here is my code

func setupRemoteCommandCenter() {
        UIApplication.shared.beginReceivingRemoteControlEvents()

        let commandCenter = MPRemoteCommandCenter.shared()
        

        commandCenter.playCommand.addTarget { event in
            return .success
        }
        

        commandCenter.pauseCommand.addTarget { event in
            return .success
        }
        

        commandCenter.nextTrackCommand.addTarget { event in
            return .success
        }
        

        commandCenter.previousTrackCommand.addTarget { event in
            return .success
        }
    }
    
    func updateLockScreen() {
        var nowPlayingInfo = [String : Any]()
        nowPlayingInfo[MPMediaItemPropertyArtist] = "Artist"
        nowPlayingInfo[MPMediaItemPropertyTitle] = "title"
        MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
    }

One method is getting called from viewDidLoad i.e

override func viewDidLoad() {
        super.viewDidLoad()
        AudioManager.shared.audioManageDelegate = self
        // Do any additional setup after loading the view.
        setupRemoteCommandCenter()
    }

And other one is getting called from playbuttonAction method i.e

@IBAction func togglePlayPauseButton(sender: UIButton) {
        //play pause button
        sender.isSelected = !sender.isSelected
        //updateLockScreen() //I checked it from calling here also
        if sender.isSelected {
            AudioManager.shared.playMusic()
        } else {
            AudioManager.shared.pauseMusic()
        }
        updateLockScreen()
    }

My Appdelegate is here

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        setUPforbackground()
        return true
    }

    func setUPforbackground() {
        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
            print("Playback OK")
            try AVAudioSession.sharedInstance().setActive(true)
            print("Session is Active")
        } catch {
            print(error)
        }
    }

Calling the updateLockScreen() before calling playMusic() also no result. Do I miss anything here?


Solution

  • After analysing code carefully I have found that

    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])

    Throwing error So I have changed it to below

    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
    

    Hope it will help someone. Thank you.