I am working on a video player which supports Picture in picture.
I play ads and video content on that player. I want to restrict automatic picture in picture when swiped up(put the app in the background) from the device when the player is playing ads. And once the ad is completed, I want to allow it to go picture in picture whenever the app goes into the background.
I am using AVPlayer
in my custom player and AVPictureInPictureController
for Picture in picture support.
I found a solution related AVPlayerViewController
in which .allowsPictureInPicturePlayback
property allows to disable picture in picture. But I didn't find any properties for AVPlayer
or AVPlayerLayer
to restrict picture in picture.
Please share possible solutions to handle this case. Thanks in advance.
AVPictureInPictureController
is responsible for showing PIP automatically when your app is putting to background and AVPlayer with AVPlayerLayer don't have any setting to manage it. So that you can create the picture in picture controller only when it is allowed to show PIP and destroy in otherwise.
For instance, you can create a property in your view controller that turn the picture in picture controller ON and OFF:
var pipController: AVPictureInPictureController?
var allowsPictureInPicturePlayback: Bool = false {
didSet {
guard AVPictureInPictureController.isPictureInPictureSupported() else { return }
if allowsPictureInPicturePlayback {
pipController = AVPictureInPictureController(playerLayer: playerLayer)
pipController?.delegate = self
}
else {
pipController = nil
}
}
}