Dismissing AVPlayerViewController doesn't 'kill' the object - it's persisting on lock screen / media controls

I am playing videos that are in my app bundle.


They are playing correctly.


However, when I call to dismiss the AVPlayerViewController, it visibly is removed from the view hierarchy but, if I turn off the iOS device and turn it back on again, on the lock screen there is a media control showing that video and a 'play' button.


If you touch play you only get the audio and no video.


My problem is I don't understand why the 'dismiss' is not completely 'killing' the player when I'm done with it.


NOTE: The follow code works perfectly in iOS 13 devices, however any device running previous iOS versions, the error appears as stated.


This is blocking me from submitting my app, please can someone advise on how to solve this?


Here is the presentation code:


  
 
    internal func play(FileName filename: String, FileType type: String)
    {
        if self.isAlreadyPlaying == YES
        {
            killVideoPlayer()
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.5, execute: { self.play(FileName: filename, FileType: type) })
            
            return
        }

        let audioSession = AVAudioSession.sharedInstance()
        
        do
        {
            try audioSession.setCategory(AVAudioSession.Category.playback, mode: .moviePlayback, options: [.allowAirPlay, .allowBluetooth, .allowBluetoothA2DP])
            try audioSession.setActive(true)
        }
        catch
        {
            print("Audio session failed")
        }
        
        let path = Bundle.main.path(forResource: filename, ofType: type)
        
        let url = NSURL(fileURLWithPath: path!)

        let player = AVPlayer(url: url as URL)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(PBFMenuSystemFloatingButtonsViewController.didFinishPlaying(notification:)),
                                               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                               object: player.currentItem)
        
        self.playerController = AVPlayerViewController()
        
        self.menuWindow.videoPlayer = self.playerController
        
        self.playerController?.player = player
        
        self.playerController?.allowsPictureInPicturePlayback = true
        
        self.playerController?.showsPlaybackControls = YES
        
        self.playerController?.delegate = self
        
        self.playerController?.exitsFullScreenWhenPlaybackEnds = YES
        
        self.isAlreadyPlaying          = YES
        
        self.present(self.playerController!, animated: true, completion : { self.playerController?.player?.play() })
    }

Here is the dismissal code:

    private func killVideoPlayer()
    {
        self.isAlreadyPlaying          = NO
        
        self.playerController?.player?.pause()
        
        self.playerController?.player  = nil
        
        let audioSession = AVAudioSession.sharedInstance()

        do
        {
            try audioSession.setActive(false, options: .notifyOthersOnDeactivation)
            try audioSession.setCategory(.soloAmbient)
        }
        catch
        {
            print("Audio session failed")
        }
        
        self.playerController?.dismiss(animated: YES, completion: { self.playerController = nil })
    }
And here's what's remaining in the systemwide media player that is shown on the lock screen / control centre:

Did you solve this ? I am also facing this issue.

Dismissing AVPlayerViewController doesn't 'kill' the object - it's persisting on lock screen / media controls
 
 
Q