enablePictureInPicture method

Future<void>? enablePictureInPicture(
  1. GlobalKey<State<StatefulWidget>> pipFlutterPlayerGlobalKey
)

Enable Picture in Picture (PiP) mode. pipFlutterPlayerGlobalKey is required to open PiP mode in iOS. When device is not supported, PiP mode won't be open.

Implementation

Future<void>? enablePictureInPicture(
    GlobalKey pipFlutterPlayerGlobalKey) 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(PipFlutterPlayerEvent(PipFlutterPlayerEventType.pipStart));
      return;
    }
    if (Platform.isIOS) {
      final RenderBox? renderBox = pipFlutterPlayerGlobalKey.currentContext!
          .findRenderObject() as RenderBox?;
      if (renderBox == null) {
        PipFlutterPlayerUtils.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 {
      PipFlutterPlayerUtils.log("Unsupported PiP in current platform.");
    }
  } else {
    PipFlutterPlayerUtils.log(
        "Picture in picture is not supported in this device. If you're "
        "using Android, please check if you're using activity v2 "
        "embedding.");
  }
}