Skip to content

Instantly share code, notes, and snippets.

@MrMeison
Created January 27, 2022 11:20
Show Gist options
  • Save MrMeison/d223bf435360b4834416f438e8d211a6 to your computer and use it in GitHub Desktop.
Save MrMeison/d223bf435360b4834416f438e8d211a6 to your computer and use it in GitHub Desktop.
export const useVideo = (videoRef: RefObject<HTMLVideoElement>, device: MediaDeviceInfo) => {
const [started, setStarted] = useState(false);
useEffect(() => {
if (!videoRef.current || !device) {
return noop;
}
const videoService = new VideoService();
const start = async () => {
try {
await videoService.start(videoRef.current, device);
setStarted(true);
} catch {
setStarted(false);
}
}
start();
return () => {
videoService.stop();
}
}, [videoRef, device]);
return started;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment