🎥 Make videos programmatically with React
align methods for video and audio into it's own effect
Jonny Burger755143e5599b
diff --git a/packages/core/src/audio/AudioForDevelopment.tsx b/packages/core/src/audio/AudioForDevelopment.tsxindex 959e0af25d..6b423f31bb 100644--- a/packages/core/src/audio/AudioForDevelopment.tsx+++ b/packages/core/src/audio/AudioForDevelopment.tsx@@ -5,6 +5,7 @@ import React, {useEffect,useImperativeHandle,useMemo,+ useRef,useState,} from 'react';import {usePreload} from '../prefetch';@@ -117,32 +118,30 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<[audioRef]);+ const currentOnDurationCallback =+ useRef<AudioForDevelopmentProps['onDuration']>();+ currentOnDurationCallback.current = onDuration;+useEffect(() => {const {current} = audioRef;if (!current) {return;}- current.onloadedmetadata = () => {- onDuration(src, current.duration);- };+ if (current.duration) {+ currentOnDurationCallback.current?.(src, current.duration);+ return;+ }- const errorHandler = () => {- if (current?.error) {- console.error('Error occurred in audio', current?.error);- throw new Error(- `The browser threw an error while playing the audio ${src}: Code ${current.error.code} - ${current?.error?.message}. See https://remotion.dev/docs/media-playback-error for help`- );- } else {- throw new Error('The browser threw an error');- }+ const onLoadedMetadata = () => {+ currentOnDurationCallback.current?.(src, current.duration);};- current.addEventListener('error', errorHandler, {once: true});+ current.addEventListener('loadedmetadata', onLoadedMetadata);return () => {- current.removeEventListener('error', errorHandler);+ current.removeEventListener('loadedmetadata', onLoadedMetadata);};- }, [audioRef, src, onDuration]);+ }, [audioRef, src]);if (initialShouldPreMountAudioElements) {return null;diff --git a/packages/core/src/video/VideoForDevelopment.tsx b/packages/core/src/video/VideoForDevelopment.tsxindex a7fbcdd11a..1fc40a4786 100644--- a/packages/core/src/video/VideoForDevelopment.tsx+++ b/packages/core/src/video/VideoForDevelopment.tsx@@ -80,10 +80,6 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<return;}- current.onloadedmetadata = () => {- onDuration(src as string, current.duration);- };-const errorHandler = () => {if (current?.error) {console.error('Error occurred in video', current?.error);@@ -99,7 +95,32 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<return () => {current.removeEventListener('error', errorHandler);};- }, [src, onDuration]);+ }, [src]);++ const currentOnDurationCallback =+ useRef<VideoForDevelopmentProps['onDuration']>();+ currentOnDurationCallback.current = onDuration;++ useEffect(() => {+ const {current} = videoRef;+ if (!current) {+ return;+ }++ if (current.duration) {+ currentOnDurationCallback.current?.(src as string, current.duration);+ return;+ }++ const onLoadedMetadata = () => {+ currentOnDurationCallback.current?.(src as string, current.duration);+ };++ current.addEventListener('loadedmetadata', onLoadedMetadata);+ return () => {+ current.removeEventListener('loadedmetadata', onLoadedMetadata);+ };+ }, [src]);return (<video