can play multiple audios, pausing the previous to execute the new one

This commit is contained in:
Reinaldo Neto 2023-08-09 19:35:20 -03:00
parent cd916644e8
commit 55afcafd22
1 changed files with 15 additions and 9 deletions

View File

@ -89,7 +89,8 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
const pauseSound = () => { const pauseSound = () => {
EventEmitter.removeListener(PAUSE_AUDIO, pauseSound); EventEmitter.removeListener(PAUSE_AUDIO, pauseSound);
togglePlayPause(); setPaused(true);
playPause(true);
}; };
const getUrl = () => { const getUrl = () => {
@ -170,6 +171,9 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
}; };
const onPress = () => { const onPress = () => {
if (loading) {
return;
}
if (cached) { if (cached) {
togglePlayPause(); togglePlayPause();
return; return;
@ -178,8 +182,6 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
}; };
useEffect(() => { useEffect(() => {
sound.current = new Audio.Sound();
const handleCache = async () => { const handleCache = async () => {
const cachedAudioResult = await getMediaCache({ const cachedAudioResult = await getMediaCache({
type: 'audio', type: 'audio',
@ -187,7 +189,8 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
urlToCache: getUrl() urlToCache: getUrl()
}); });
if (cachedAudioResult?.exists) { 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); setLoading(false);
setCached(true); setCached(true);
return; return;
@ -202,12 +205,15 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
return () => { return () => {
EventEmitter.removeListener(PAUSE_AUDIO, pauseSound); EventEmitter.removeListener(PAUSE_AUDIO, pauseSound);
const unloadAsync = async () => {
try { try {
sound.current?.stopAsync(); await sound.current?.unloadAsync();
} catch { } catch {
// Do nothing // Do nothing
} }
}; };
unloadAsync();
};
}, []); }, []);
useEffect(() => { useEffect(() => {