setPresentationMode method

  1. @override
void setPresentationMode(
  1. PresentationMode presentationMode,
  2. AutomaticFullscreenExitListener? automaticFullscreenExitListener
)
override

Implementation

@override
void setPresentationMode(PresentationMode presentationMode, AutomaticFullscreenExitListener? automaticFullscreenExitListener) {
  // _playerWrapperDiv only contains the video view, but we need the whole document to go into fullscreen
  // so we first present the fullscreen widget (with or without UI) then we trigger fullscreen.
  var elementToFullscreen = document.documentElement as HTMLElement;

  var previousPresentationMode = currentPresentationMode;
  currentPresentationMode = presentationMode;

  switch (presentationMode) {
    case PresentationMode.FULLSCREEN:
      elementToFullscreen?.requestFullscreen();

      jsFullscreenChangeListener = ((JSAny event) {
        if (document.fullscreenElement != null) {
          if (kDebugMode) {
            print(
              'Element: ${document.fullscreenElement} entered fullscreen mode.',
            );
          }
        } else {
          if (kDebugMode) {
            print('Leaving fullscreen mode.');
          }
          if (jsFullscreenChangeListener != null) {
            elementToFullscreen.removeEventListener(
              WebEventTypes.FULLSCREEN_CHANGE,
              jsFullscreenChangeListener,
            );
          }
          automaticFullscreenExitListener?.call();
        }
      }).toJS;

      elementToFullscreen.addEventListener(
        WebEventTypes.FULLSCREEN_CHANGE,
        jsFullscreenChangeListener,
      );
    case PresentationMode.INLINE:
      if (previousPresentationMode == PresentationMode.FULLSCREEN) {
        if (jsFullscreenChangeListener != null) {
          elementToFullscreen.removeEventListener(
            WebEventTypes.FULLSCREEN_CHANGE,
            jsFullscreenChangeListener,
          );
        }
        //TOOD: check previous presentation mode
        if (document.fullscreenElement != null) {
          document.exitFullscreen();
        }
      } else if (previousPresentationMode == PresentationMode.PIP) {
        if (document.pictureInPictureElement != null) {
          if (kDebugMode) {
            print('Exit pip');
          }

          try {
            document.exitPictureInPicture();
          } catch (e) {
            print('Error when exitPictureInPicture(), $e');
          }
        } else {
          if (kDebugMode) {
            print('We were in PiP, but no pictureInPictureElement !?!?');
          }
        }
      }
    case PresentationMode.PIP: {

      HTMLVideoElement? videoElement = _getPlayingVideoElement();

      jsPiPEnterListener = ((JSAny event) {
        if (document.pictureInPictureElement != null) {
          if (kDebugMode) {
            print(
              'Element: ${document.pictureInPictureElement} entered PiP mode.',
            );
          }
        } else {
          if (kDebugMode) {
            print('ERROR entering PiP mode.');
          }
        }
      }).toJS;

      jsPiPLeaveListener =((JSAny event) {

          if (kDebugMode) {
            print('Leaving PiP mode.');
          }
          if (jsPiPEnterListener != null) {
            elementToFullscreen.removeEventListener(
              WebEventTypes.PICTUREINPICTURE_ENTER,
              jsPiPEnterListener,
            );
          }

          if (jsPiPLeaveListener != null) {
            elementToFullscreen.removeEventListener(
              WebEventTypes.PICTUREINPICTURE_EXIT,
              jsPiPLeaveListener,
            );
          }

          automaticFullscreenExitListener?.call();

      }).toJS;

      videoElement?.addEventListener(
        WebEventTypes.PICTUREINPICTURE_ENTER,
        jsPiPEnterListener,
      );

      videoElement?.addEventListener(
        WebEventTypes.PICTUREINPICTURE_EXIT,
        jsPiPLeaveListener,
      );

      //NOTE: videoElement.requestPictureInPicture() doesn't work
      try {
        videoElement?.callMethod("requestPictureInPicture".toJS);
      } catch (e) {
        print('Error when requestPictureInPicture() , $e');
      }

    }
    default:
      print("Unsupported presentationMode $presentationMode");
  }
}