enablePictureInPicture method
Enable Picture in Picture (PiP) mode. betterPlayerGlobalKey
is required
to open PiP mode in iOS. When device is not supported, PiP mode won't be
open.
Implementation
Future<void>? enablePictureInPicture(GlobalKey betterPlayerGlobalKey) async {
if (videoPlayerController == null) {
throw StateError("The data source has not been initialized");
}
final bool isPipSupported =
(await videoPlayerController!.isPictureInPictureSupported()) ?? false;
if (isPipSupported) {
_wasInFullScreenBeforePiP = _isFullScreen;
_wasControlsEnabledBeforePiP = _controlsEnabled;
setControlsEnabled(false);
if (Platform.isAndroid) {
_wasInFullScreenBeforePiP = _isFullScreen;
await videoPlayerController?.enablePictureInPicture(
left: 0, top: 0, width: 0, height: 0);
enterFullScreen();
_postEvent(BetterPlayerEvent(BetterPlayerEventType.pipStart));
return;
}
if (Platform.isIOS) {
final RenderBox? renderBox = betterPlayerGlobalKey.currentContext!
.findRenderObject() as RenderBox?;
if (renderBox == null) {
BetterPlayerUtils.log(
"Can't show PiP. RenderBox is null. Did you provide valid global"
" key?");
return;
}
final Offset position = renderBox.localToGlobal(Offset.zero);
return videoPlayerController?.enablePictureInPicture(
left: position.dx,
top: position.dy,
width: renderBox.size.width,
height: renderBox.size.height,
);
} else {
BetterPlayerUtils.log("Unsupported PiP in current platform.");
}
} else {
BetterPlayerUtils.log(
"Picture in picture is not supported in this device. If you're "
"using Android, please check if you're using activity v2 "
"embedding.");
}
}