From 55afcafd22670bd537523f44835e78b436d27483 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Wed, 9 Aug 2023 19:35:20 -0300 Subject: [PATCH] can play multiple audios, pausing the previous to execute the new one --- .../message/Components/Audio/index.tsx | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/app/containers/message/Components/Audio/index.tsx b/app/containers/message/Components/Audio/index.tsx index 87d59361c..716427338 100644 --- a/app/containers/message/Components/Audio/index.tsx +++ b/app/containers/message/Components/Audio/index.tsx @@ -89,7 +89,8 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage const pauseSound = () => { EventEmitter.removeListener(PAUSE_AUDIO, pauseSound); - togglePlayPause(); + setPaused(true); + playPause(true); }; const getUrl = () => { @@ -170,6 +171,9 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage }; const onPress = () => { + if (loading) { + return; + } if (cached) { togglePlayPause(); return; @@ -178,8 +182,6 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage }; useEffect(() => { - sound.current = new Audio.Sound(); - const handleCache = async () => { const cachedAudioResult = await getMediaCache({ type: 'audio', @@ -187,7 +189,8 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage urlToCache: getUrl() }); if (cachedAudioResult?.exists) { - await sound.current?.loadAsync({ uri: cachedAudioResult.uri }); + const { sound: soundLoaded } = await Audio.Sound.createAsync({ uri: cachedAudioResult.uri }); + sound.current = soundLoaded; setLoading(false); setCached(true); return; @@ -202,11 +205,14 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage return () => { EventEmitter.removeListener(PAUSE_AUDIO, pauseSound); - try { - sound.current?.stopAsync(); - } catch { - // Do nothing - } + const unloadAsync = async () => { + try { + await sound.current?.unloadAsync(); + } catch { + // Do nothing + } + }; + unloadAsync(); }; }, []);