How to vanish player controls from lockscreen after the playback has finished in iOS10+?

3.4k Views Asked by At

While playing audio in background mode player controls appears on the lockscreen. How to remove it when the audio has stopped? If try to set:

MPNowPlayingInfoCenter.default().nowPlayingInfo = nil

player is still on the lockscreen, but the fields artist/song are empty

enter image description here

UPD (my code for audiosession):

in AppDelegate:

func setupAudioSession() {

    let audioSession = AVAudioSession.sharedInstance()

    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayback)
        try audioSession.setActive(true)

    } catch {
        print("Setting category to AVAudioSessionCategoryPlayback failed.")
    }
}

in Player class:

private func clearRemotePlayerInfo() { // call after stop button pressed
    try? AVAudioSession.sharedInstance().setActive(false)
    MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
}
2

There are 2 best solutions below

9
Jakub On BEST ANSWER

TL;DR

Example on Github: https://github.com/JakubMazur/SO52243428


You shouldn't assign nil to this nowPlayingInfo.

What you should do to achieve this is:

  1. Stop your playback (not necessary but it's good to clean up what you've created)
  2. Set your session inactive
  3. Clear nowPlayingInfo

So the code will look like:

self.player?.stop() // Where self.player is AVAudioPlayer
try? self.session?.setActive(false, with: .notifyOthersOnDeactivation) // Where self.session is AVAudioSession. You should do it with do-catch to good error catching.
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]

And it will behave like this:

enter image description here

EDIT:

I've wrote as simple as possible example for try it out. It's available on Github https://github.com/JakubMazur/SO52243428. Feel free to check it out and it it matches your case.

0
Jake On

It's work for me.

func setupNowPlaying() {
    var nowPlayingInfo = [String : Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = "my video"
    if let image = UIImage(named: "video image") {
        nowPlayingInfo[MPMediaItemPropertyArtwork] =
            MPMediaItemArtwork(boundsSize: image.size) { size in
                return image
        }
    }
    let playerItem = self.player?.currentItem
    
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playerItem?.currentTime().seconds
    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = playerItem?.asset.duration.seconds
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = self.player?.rate
 
    
    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
    
    UIApplication.shared.beginReceivingRemoteControlEvents()
    
}


func closeVideo() {
    self.player?.pause()
    
    let sessionAV = AVAudioSession.sharedInstance()
    try? sessionAV.setActive(false, options: AVAudioSession.SetActiveOptions.notifyOthersOnDeactivation)
    
    
    MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
    
    UIApplication.shared.endReceivingRemoteControlEvents()
}