From cce24cd829eb5701af36672465990431381d9dd9 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 30 Jun 2024 13:25:49 +0000 Subject: [PATCH] Add media session support --- src/Video.web.tsx | 281 ++++++++++++++++++++---------- src/specs/VideoNativeComponent.ts | 4 +- src/types/video-ref.ts | 3 + 3 files changed, 198 insertions(+), 90 deletions(-) diff --git a/src/Video.web.tsx b/src/Video.web.tsx index e2defb4b..8c150ae8 100644 --- a/src/Video.web.tsx +++ b/src/Video.web.tsx @@ -4,8 +4,10 @@ import React, { useEffect, useImperativeHandle, useRef, + type RefObject, + useMemo, } from 'react'; -import type {VideoRef, ReactVideoProps} from './types'; +import type {VideoRef, ReactVideoProps, VideoMetadata} from './types'; const Video = forwardRef( ( @@ -17,7 +19,7 @@ const Video = forwardRef( rate, repeat, controls, - showNotificationControls, + showNotificationControls = false, poster, onBuffer, onLoad, @@ -45,6 +47,7 @@ const Video = forwardRef( console.warn('Video Component is not mounted'); return; } + time = Math.max(0, Math.min(time, nativeRef.current.duration)); nativeRef.current.currentTime = time; onSeek?.({seekTime: time, currentTime: nativeRef.current.currentTime}); }, @@ -98,8 +101,17 @@ const Video = forwardRef( setFullScreen: unsupported, save: unsupported, restoreUserInterfaceForPictureInPictureStopCompleted: unsupported, + nativeHtmlRef: nativeRef, }), - [seek, pause, resume, unsupported, setVolume, getCurrentPosition], + [ + seek, + pause, + resume, + unsupported, + setVolume, + getCurrentPosition, + nativeRef, + ], ); useEffect(() => { @@ -137,95 +149,188 @@ const Video = forwardRef( nativeRef.current.playbackRate = rate; }, [rate]); + useMediaSession( + source?.metadata, + nativeRef, + useMemo( + () => ({ + seek, + resume, + pause, + }), + [seek, resume, pause], + ), + showNotificationControls, + ); + return ( - <> - {showNotificationControls && ( - - )} -