diff --git a/src/components/MediaMaker/RenderControls/RenderImageButton.tsx b/src/components/MediaMaker/RenderControls/RenderImageButton.tsx index 293e6bc673..a7b3cfc967 100644 --- a/src/components/MediaMaker/RenderControls/RenderImageButton.tsx +++ b/src/components/MediaMaker/RenderControls/RenderImageButton.tsx @@ -1,5 +1,6 @@ -import React, { useEffect, useRef } from 'react'; +import React, { MutableRefObject, useCallback, useEffect, useRef } from 'react'; +import { PlayerRef } from '@remotion/player'; import { useRouter } from 'next/router'; import useTranslation from 'next-translate/useTranslation'; @@ -18,23 +19,21 @@ import { getLoginNavigationUrl, getQuranMediaMakerNavigationUrl } from '@/utils/ type Props = { inputProps: any; - getCurrentFrame: () => number; - getIsPlayerPlaying: () => boolean; + playerRef: MutableRefObject; isFetching: boolean; }; -const RenderImageButton: React.FC = ({ - inputProps, - getCurrentFrame, - getIsPlayerPlaying, - isFetching, -}) => { +const RenderImageButton: React.FC = ({ inputProps, playerRef, isFetching }) => { const { t } = useTranslation('media'); const { renderMedia, state, undo } = useGenerateMediaFile(inputProps); const { data, mutate } = useGetMediaFilesCount(MediaType.IMAGE); const previousFrame = useRef(); const router = useRouter(); const downloadButtonRef = React.useRef(); + const shouldRerender = useRef(false); + + const getCurrentFrame = useCallback(() => playerRef?.current?.getCurrentFrame(), [playerRef]); + const getIsPlayerPlaying = () => playerRef?.current?.isPlaying(); const triggerRenderImage = () => { const frame = getCurrentFrame(); @@ -65,7 +64,10 @@ const RenderImageButton: React.FC = ({ logButtonClick('download_image'); const isFrameDifferent = previousFrame.current !== getCurrentFrame(); const isPlaying = getIsPlayerPlaying(); - if (isPlaying && isFrameDifferent) { + if (shouldRerender.current) { + undo(); + shouldRerender.current = false; + } else if (isPlaying && isFrameDifferent) { undo(); } else if (!isPlaying && isFrameDifferent) { e.preventDefault(); @@ -78,6 +80,11 @@ const RenderImageButton: React.FC = ({ // listen to state changes and download the file when it's done useEffect(() => { if (state?.status === RenderStatus.DONE) { + const isFrameDifferent = previousFrame.current !== getCurrentFrame(); + if (isFrameDifferent) { + shouldRerender.current = true; + } + previousFrame.current = getCurrentFrame(); downloadButtonRef.current.click(); mutate(mutateGeneratedMediaCounter, { revalidate: false }); } diff --git a/src/components/MediaMaker/RenderControls/index.tsx b/src/components/MediaMaker/RenderControls/index.tsx index 52c3f50b8d..0011ee0d35 100644 --- a/src/components/MediaMaker/RenderControls/index.tsx +++ b/src/components/MediaMaker/RenderControls/index.tsx @@ -1,5 +1,6 @@ -import React, { useEffect, useState } from 'react'; +import React, { MutableRefObject, useEffect, useState } from 'react'; +import { PlayerRef } from '@remotion/player'; import classNames from 'classnames'; import clipboardCopy from 'clipboard-copy'; import useTranslation from 'next-translate/useTranslation'; @@ -16,9 +17,8 @@ import { getCurrentPath } from '@/utils/url'; type Props = { inputProps: MediaFileCompositionProps; - getCurrentFrame: () => number; - getIsPlayerPlaying: () => boolean; isFetching: boolean; + playerRef: MutableRefObject; }; export type MediaFileCompositionProps = { @@ -34,12 +34,7 @@ export type MediaFileCompositionProps = { const COPY_TIMEOUT_MS = 3000; -const RenderControls: React.FC = ({ - inputProps, - getCurrentFrame, - getIsPlayerPlaying, - isFetching, -}) => { +const RenderControls: React.FC = ({ inputProps, isFetching, playerRef }) => { const { t } = useTranslation('media'); const [isCopied, setIsCopied] = useState(false); @@ -74,12 +69,7 @@ const RenderControls: React.FC = ({
- +
- + ); }; diff --git a/src/pages/media/index.tsx b/src/pages/media/index.tsx index ffa846a370..25d793cf27 100644 --- a/src/pages/media/index.tsx +++ b/src/pages/media/index.tsx @@ -83,7 +83,6 @@ const MediaMaker: NextPage = ({ const [isReady, setIsReady] = useState(false); const [videoFileReady, setVideoFileReady] = useState(false); const [audioFileReady, setAudioFileReady] = useState(false); - const [isUpdating, setIsUpdating] = useState(false); const TOAST_GENERAL_ERROR = t('common:error.general'); const areMediaFilesReady = videoFileReady && audioFileReady; @@ -138,17 +137,6 @@ const MediaMaker: NextPage = ({ useAddQueryParamsToUrl(getQuranMediaMakerNavigationUrl(queryParams), {}); - const seekToBeginning = useCallback(() => { - const current = playerRef?.current; - if (!current) { - return; - } - if (current.isPlaying) { - current.pause(); - } - current.seekTo(0); - }, []); - useEffect(() => { const current = playerRef?.current; if (!current) { @@ -159,17 +147,13 @@ const MediaMaker: NextPage = ({ current.toggle(e); }; - current.getContainerNode().addEventListener('click', onClick); + current?.getContainerNode().addEventListener('click', onClick); // eslint-disable-next-line consistent-return return () => { - current.getContainerNode().removeEventListener('click', onClick); + current?.getContainerNode().removeEventListener('click', onClick); }; }, []); - useEffect(() => { - setIsUpdating(false); - }, [mediaSettings]); - const API_PARAMS = useMemo(() => { return { ...DEFAULT_API_PARAMS, @@ -248,14 +232,6 @@ const MediaMaker: NextPage = ({ return englishChaptersList?.[surah]?.translatedName as string; }, [surah, englishChaptersList]); - const getCurrentFrame = useCallback(() => { - return playerRef?.current?.getCurrentFrame(); - }, []); - - const getIsPlayerPlaying = useCallback(() => { - return playerRef?.current?.isPlaying(); - }, []); - const audioData = useMemo(() => { return getCurrentRangesAudioData(currentSurahAudioData, Number(verseFrom), Number(verseTo)); }, [currentSurahAudioData, verseFrom, verseTo]); @@ -332,7 +308,6 @@ const MediaMaker: NextPage = ({ // {@see https://www.remotion.dev/docs/troubleshooting/player-flicker#option-6-prefetching-as-base64-to-avoid-network-request-and-local-http-server} const { waitUntilDone: waitUntilAudioDone } = prefetch(inputProps.audio.audioUrl, { method: 'blob-url', - contentType: 'audio/mp3', }); waitUntilAudioDone() @@ -358,7 +333,7 @@ const MediaMaker: NextPage = ({ const renderPoster: RenderPoster = useCallback(() => { const video = getBackgroundVideoById(videoId); - if (isUpdating || isFetching || !areMediaFilesReady) { + if (isFetching || !areMediaFilesReady) { return (
@@ -377,7 +352,7 @@ const MediaMaker: NextPage = ({ /> ); - }, [areMediaFilesReady, isFetching, isUpdating, videoId]); + }, [areMediaFilesReady, isFetching, videoId]); const chaptersList = useMemo(() => { return Object.entries(chaptersData).map(([id, chapterObj], index) => ({ @@ -428,7 +403,7 @@ const MediaMaker: NextPage = ({ doubleClickToFullscreen fps={VIDEO_FPS} ref={playerRef} - controls={!isUpdating && !isFetching && areMediaFilesReady} + controls={!isFetching && areMediaFilesReady} bufferStateDelayInMilliseconds={200} // wait for 200ms second before showing the spinner renderPoster={renderPoster} posterFillMode="player-size" @@ -443,13 +418,10 @@ const MediaMaker: NextPage = ({