2

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.

Here is the presentation code:

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(VideoLibraryViewController.didFinishPlaying(notification:)),
                                       name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                       object: player.currentItem)

self.playerController = AVPlayerViewController()

self.playerController?.player = player

self.playerController?.allowsPictureInPicturePlayback = true

self.playerController?.showsPlaybackControls = YES

self.playerController?.delegate = self

self.playerController?.player?.play()

self.present(self.playerController!, animated: true, completion : nil)

Here is the dismissal code:


// Delegate can implement this method to be notified when Picture in Picture will start.
func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
    self.playerController?.dismiss(animated: NO, completion: nil )
}

And here's what's remaining in the systemwide media player that is shown on the lock screen / control centre:

enter image description here

iOSProgrammingIsFun
  • 1,418
  • 1
  • 15
  • 32

1 Answers1

2

iOS 13 SDK ONLY: Here's the solution, but the answer is that despite dismissing the AVPlayerViewController, the AVPlayer object that it's knows about is persistent and that needs to be set to nil.

private func killVideoPlayer()
{ 
    self.playerController?.player?.pause()
    self.playerController?.player  = nil

    self.playerController?.dismiss(animated: YES, completion: { self.playerController = nil })
}

Previous SDK's, this still isn't working.

Neither is setting the AVAudioSession.active to false... ?!?! Still need a pre iOS 13 SDK solution.

iOSProgrammingIsFun
  • 1,418
  • 1
  • 15
  • 32