-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #400 from AppQuality/player-pip
feat: Add Picture-in-Picture functionality to Player component
- Loading branch information
Showing
4 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { useEffect } from "react"; | ||
import { PlayerArgs } from "../_types"; | ||
|
||
type PictureInPictureHook = ( | ||
videoRef?: HTMLVideoElement | null, | ||
pipMode?: PlayerArgs["pipMode"], | ||
onPipChange?: PlayerArgs["onPipChange"] | ||
) => void; | ||
|
||
export const usePictureInPicture: PictureInPictureHook = (videoRef, pipMode, onPipChange) => { | ||
|
||
const getObserver = (videoRef: HTMLVideoElement, isVideoPlaying: () => boolean) => { | ||
return new IntersectionObserver( | ||
(entries) => { | ||
entries.forEach((entry) => { | ||
if (!entry.isIntersecting && isVideoPlaying()) { | ||
videoRef.requestPictureInPicture(); | ||
} | ||
if ( | ||
document.pictureInPictureElement && | ||
entry.isIntersecting && | ||
!isVideoPlaying() | ||
) { | ||
document.exitPictureInPicture(); | ||
} | ||
}); | ||
}, | ||
{ threshold: 0.5 } | ||
); | ||
}; | ||
|
||
const handleManualPipMode = (videoRef: HTMLVideoElement, pipMode: boolean) => { | ||
if (pipMode) { | ||
videoRef.requestPictureInPicture(); | ||
} | ||
if (!pipMode && document.pictureInPictureElement) { | ||
document.exitPictureInPicture(); | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
// bail out if pipMode is not defined or videoRef is not defined or pip is not supported | ||
if (typeof pipMode === "undefined") | ||
return; | ||
if (!document.pictureInPictureEnabled) { | ||
console.warn("Picture-in-Picture is not supported in this browser"); | ||
return; | ||
} | ||
if (!videoRef) | ||
return; | ||
|
||
// if pipMode is auto, we will enter picture in picture mode when the video is not in view | ||
if (pipMode === "auto") { | ||
const isVideoPlaying = () => | ||
videoRef.currentTime > 0 && | ||
!videoRef.paused && | ||
!videoRef.ended && | ||
videoRef.readyState > 2; | ||
|
||
const observer = getObserver(videoRef, isVideoPlaying); | ||
observer.observe(videoRef); | ||
|
||
return () => { | ||
observer.disconnect(); | ||
}; | ||
} else if (typeof pipMode === "boolean") { | ||
handleManualPipMode(videoRef, pipMode); | ||
} else { | ||
handleManualPipMode(videoRef, pipMode()); | ||
} | ||
}, [pipMode, videoRef]); | ||
|
||
useEffect(() => { | ||
if (!document.pictureInPictureEnabled) { | ||
return; | ||
} | ||
document.addEventListener("enterpictureinpicture", () => { | ||
onPipChange?.(true); | ||
}); | ||
document.addEventListener("leavepictureinpicture", () => { | ||
onPipChange?.(false); | ||
}); | ||
|
||
return () => { | ||
document.removeEventListener("enterpictureinpicture", () => { | ||
onPipChange?.(true); | ||
}); | ||
document.removeEventListener("leavepictureinpicture", () => { | ||
onPipChange?.(false); | ||
}); | ||
}; | ||
}, [onPipChange]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters