AVPlayerViewController black screen when swiping on iOS 11

heyfrank picture heyfrank · Sep 20, 2017 · Viewed 7.5k times · Source

I'm using AVPlayerViewController to play a video file (H.264, AAC, MP4-Container) on an iPad-App. Everything is working in iOS 10. And also in iOS 11 it plays the video correctly.

But in iOS 11, when I start swiping in any direction, it immediately blacks out the video and also mutes audio. It also shows a loading indicator next to the timeline on the bottom.

Also it ignores the allowsPictureInPicturePlayback property, so it doesn't show the PIP-Button on iOS 11.

This is the code I use:

avPlayerController = AVPlayerViewController()
avPlayerController?.showsPlaybackControls = true
avPlayerController?.allowsPictureInPicturePlayback = true
avPlayerController?.player = AVPlayer(url: videoUrl as URL)
avPlayerController?.player?.play()            

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

avPlayerController?.player?.actionAtItemEnd = AVPlayerActionAtItemEnd.none
NotificationCenter.default.addObserver(self, selector: #selector(onVideoCompleted), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.avPlayerController!.player?.currentItem)

And this function to close the Video Player at the end of the video:

func onVideoCompleted(notification:Notification) {
    self.avPlayerController?.player = nil
    self.avPlayerController?.dismiss(animated: true, completion: nil)
}

When the screen blacks out I get this in the Console:

AVOutputDeviceDiscoverySession (FigRouteDiscoverer) 
>>>> -[AVFigRouteDiscovererOutputDeviceDiscoverySessionImpl 
outputDeviceDiscoverySessionDidChangeDiscoveryMode:]: Setting device discovery 
mode to DiscoveryMode_Presence (client: MyAppName)

Answer

heyfrank picture heyfrank · Sep 20, 2017

Okay I found the mistake: To close the Airplay video playback when pressing "Done" I used this code:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if avPlayerController?.isBeingDismissed ?? false {
        avPlayerController?.player = nil
    }
}

But with iOS 11, Apple added a feature to close the Video Player via a swipe gesture. So when I swipe, the viewWillAppear function gets called. Putting this code inside viewDidAppear fixed this and preserved the AirPlay-Fix.