I'm using AVPlayerViewController
and have set canStartPictureInPictureAutomaticallyFromInline = true
which works as expected.
What i'd like to achieve, is if the app is opened whilst a video is in PiP mode, I want to automatically dismiss the PiP to put the video back into the AVPlayerViewController
, as currently the video stays in PiP and the in-app video player shows the PiP placeholder.
I can listen to the delegate callbacks in AVPlayerViewControllerDelegate
to know when PiP is active, but I can't figure out how to dismiss it.
AVPictureInPictureController
has a stopPictureInPicture()
function, but I don't know how to add an AVPictureInPictureController
to my player.
I use an AVPlayer
instance passed into the AVPlayerViewController
but from what i'm seeing, I need an AVPlayerLayer
to pass into AVPictureInPictureController
.
Simply toggle allowsPictureInPicturePlayback
and toggle it back when the app becomes active like this:
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
.sink { [weak self] _ in
self?.viewController.allowsPictureInPicturePlayback = false
self?.viewController.allowsPictureInPicturePlayback = true
}
.store(in: &cancellables)
This might seem like an incorrect approach, but it works for me with seamless animation.