I'm trying to implement PictureInPicture mode in Flutter. For that I'm writing a native iOS view using Swift. When app goes to background and PiP mode starts, I have the following error:
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<Runner.FLEmbededViewController: 0x12503ca00> should have parent view controller:<FlutterViewController: 0x109021600> but requested parent is:<AVPictureInPictureViewController: 0x12bc5b5b0>'
My code:
@available(iOS 15.0, *)
class FLEmbededViewController: AVPictureInPictureVideoCallViewController, AVPictureInPictureControllerDelegate {
var remoteVideoView: VTVideoView!
var pipController: AVPictureInPictureController!
override func viewDidLoad() {
super.viewDidLoad()
initPiP()
VoxeetSDK.shared.conference.delegate = self
}
func initPiP() {
if #available(iOS 15.0, *) {
if AVPictureInPictureController.isPictureInPictureSupported() {
let sampleBufferVideoCallView = SampleBufferVideoCallView()
view.addSubview(sampleBufferVideoCallView)
sampleBufferVideoCallView.translatesAutoresizingMaskIntoConstraints = false
let constraints = [
sampleBufferVideoCallView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
sampleBufferVideoCallView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
sampleBufferVideoCallView.topAnchor.constraint(equalTo: view.topAnchor),
sampleBufferVideoCallView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
]
NSLayoutConstraint.activate(constraints)
let pipContentSource = AVPictureInPictureController.ContentSource(activeVideoCallSourceView: remoteVideoView, contentViewController: self)
pipController = AVPictureInPictureController(contentSource: pipContentSource)
pipController.canStartPictureInPictureAutomaticallyFromInline = true
pipController.delegate = self
}
}
}
func createRemoteVideoView() {
remoteVideoView = VTVideoView()
view.addSubview(remoteVideoView)
remoteVideoView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
remoteVideoView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
remoteVideoView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
remoteVideoView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
remoteVideoView.topAnchor.constraint(equalTo: view.topAnchor),
])
remoteVideoView.backgroundColor = .black
}
}
class SampleBufferVideoCallView: UIView {
override class var layerClass: AnyClass {
AVSampleBufferDisplayLayer.self
}
var sampleBufferDisplayLayer: AVSampleBufferDisplayLayer {
layer as! AVSampleBufferDisplayLayer
}
}
From what the error says I should change parent for FLEmbededViewController
, but I don't really know how to do it.