diff --git a/src/components/DailyVideo.tsx b/src/components/DailyVideo.tsx index aaf112e..c079150 100644 --- a/src/components/DailyVideo.tsx +++ b/src/components/DailyVideo.tsx @@ -167,13 +167,13 @@ export const DailyVideo = forwardRef( if (!onResize || !video) return; let frame: ReturnType; - const handleResize = () => { - if (!video) return; + function handleResize() { if (frame) cancelAnimationFrame(frame); frame = requestAnimationFrame(() => { - if (document.hidden) return; - const videoWidth = video?.videoWidth; - const videoHeight = video?.videoHeight; + const video = videoEl.current; + if (!video || document.hidden) return; + const videoWidth = video.videoWidth; + const videoHeight = video.videoHeight; if (videoWidth && videoHeight) { onResize({ aspectRatio: videoWidth / videoHeight, @@ -182,14 +182,19 @@ export const DailyVideo = forwardRef( }); } }); - }; + } handleResize(); - video?.addEventListener('resize', handleResize); + video.addEventListener('loadedmetadata', handleResize); + video.addEventListener('resize', handleResize); - return () => video?.removeEventListener('resize', handleResize); + return () => { + if (frame) cancelAnimationFrame(frame); + video.removeEventListener('loadedmetadata', handleResize); + video.removeEventListener('resize', handleResize); + }; }, - [onResize, videoTrack] + [onResize] ); return (