From 74d1add9c114cef9862b6ec11314897f0d4594d3 Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Thu, 27 Mar 2025 00:18:48 +0100 Subject: [PATCH] fix: audio component recursive loading for expo --- .../components/Attachment/AudioAttachment.tsx | 12 ++-- package/src/hooks/useAudioPlayer.ts | 68 ++++++++++--------- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/package/src/components/Attachment/AudioAttachment.tsx b/package/src/components/Attachment/AudioAttachment.tsx index 56706803fa..f2b5333c38 100644 --- a/package/src/components/Attachment/AudioAttachment.tsx +++ b/package/src/components/Attachment/AudioAttachment.tsx @@ -208,10 +208,14 @@ export const AudioAttachment = (props: AudioAttachmentProps) => { if (!isExpoCLI) { return; } - if (item.paused) { - await pauseAudio(); - } else { - await playAudio(); + try { + if (item.paused) { + await pauseAudio(); + } else { + await playAudio(); + } + } catch (e) { + console.log('An error has occurred while trying to interact with the audio. ', e); } }; // For expo CLI diff --git a/package/src/hooks/useAudioPlayer.ts b/package/src/hooks/useAudioPlayer.ts index eb28bd97f4..9ac586132e 100644 --- a/package/src/hooks/useAudioPlayer.ts +++ b/package/src/hooks/useAudioPlayer.ts @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useCallback } from 'react'; import { NativeHandlers, SoundReturnType } from '../native'; @@ -15,7 +15,7 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => { const isExpoCLI = NativeHandlers.SDK === 'stream-chat-expo'; - const playAudio = async () => { + const playAudio = useCallback(async () => { if (isExpoCLI) { if (soundRef.current?.playAsync) { await soundRef.current.playAsync(); @@ -25,9 +25,9 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => { soundRef.current.resume(); } } - }; + }, [isExpoCLI, soundRef]); - const pauseAudio = async () => { + const pauseAudio = useCallback(async () => { if (isExpoCLI) { if (soundRef.current?.pauseAsync) { await soundRef.current.pauseAsync(); @@ -37,39 +37,45 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => { soundRef.current.pause(); } } - }; + }, [isExpoCLI, soundRef]); - const seekAudio = async (currentTime: number) => { - if (isExpoCLI) { - if (currentTime === 0) { - // If currentTime is 0, we should replay the video from 0th position. - if (soundRef.current?.replayAsync) { - await soundRef.current.replayAsync({ - positionMillis: 0, - shouldPlay: false, - }); + const seekAudio = useCallback( + async (currentTime: number) => { + if (isExpoCLI) { + if (currentTime === 0) { + // If currentTime is 0, we should replay the video from 0th position. + if (soundRef.current?.replayAsync) { + await soundRef.current.replayAsync({ + positionMillis: 0, + shouldPlay: false, + }); + } + } else { + if (soundRef.current?.setPositionAsync) { + await soundRef.current.setPositionAsync(currentTime * 1000); + } } } else { - if (soundRef.current?.setPositionAsync) { - await soundRef.current.setPositionAsync(currentTime * 1000); + if (soundRef.current?.seek) { + soundRef.current.seek(currentTime); } } - } else { - if (soundRef.current?.seek) { - soundRef.current.seek(currentTime); - } - } - }; + }, + [isExpoCLI, soundRef], + ); - const changeAudioSpeed = async (speed: number) => { - // Handled through prop `rate` in `Sound.Player` - if (!isExpoCLI) { - return; - } - if (soundRef.current?.setRateAsync) { - await soundRef.current.setRateAsync(speed); - } - }; + const changeAudioSpeed = useCallback( + async (speed: number) => { + // Handled through prop `rate` in `Sound.Player` + if (!isExpoCLI) { + return; + } + if (soundRef.current?.setRateAsync) { + await soundRef.current.setRateAsync(speed); + } + }, + [isExpoCLI, soundRef], + ); return { changeAudioSpeed, pauseAudio, playAudio, seekAudio }; };