fix togglePlayPause

This commit is contained in:
Reinaldo Neto 2023-08-04 18:39:23 -03:00
parent 29b61e0ef3
commit c469d0f8e9
1 changed files with 25 additions and 25 deletions

View File

@ -3,7 +3,7 @@ import { StyleProp, StyleSheet, Text, TextStyle, View, useWindowDimensions } fro
import { Audio, AVPlaybackStatus, InterruptionModeAndroid, InterruptionModeIOS } from 'expo-av'; import { Audio, AVPlaybackStatus, InterruptionModeAndroid, InterruptionModeIOS } from 'expo-av';
import Slider from '@react-native-community/slider'; import Slider from '@react-native-community/slider';
import moment from 'moment'; import moment from 'moment';
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake'; import { activateKeepAwakeAsync, deactivateKeepAwake } from 'expo-keep-awake';
import { Sound } from 'expo-av/build/Audio/Sound'; import { Sound } from 'expo-av/build/Audio/Sound';
import Touchable from './Touchable'; import Touchable from './Touchable';
@ -175,6 +175,23 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
const togglePlayPause = () => { const togglePlayPause = () => {
setPaused(!paused); setPaused(!paused);
playPause(!paused);
};
const playPause = async (isPaused: boolean) => {
try {
if (isPaused) {
await sound.current?.pauseAsync();
EventEmitter.removeListener(PAUSE_AUDIO, pauseSound);
} else {
EventEmitter.emit(PAUSE_AUDIO);
EventEmitter.addEventListener(PAUSE_AUDIO, pauseSound);
await Audio.setAudioModeAsync(mode);
await sound.current?.playAsync();
}
} catch {
// Do nothing
}
}; };
const handleDownload = async () => { const handleDownload = async () => {
@ -235,17 +252,7 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
useEffect(() => { useEffect(() => {
sound.current = new Audio.Sound(); sound.current = new Audio.Sound();
sound.current?.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate); sound.current?.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate);
return () => {
EventEmitter.removeListener(PAUSE_AUDIO, pauseSound);
try {
sound.current?.stopAsync();
} catch {
// Do nothing
}
};
}, []);
useEffect(() => {
const handleCache = async () => { const handleCache = async () => {
const cachedAudioResult = await getMediaCache({ const cachedAudioResult = await getMediaCache({
type: 'audio', type: 'audio',
@ -265,29 +272,22 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
await handleAutoDownload(); await handleAutoDownload();
}; };
handleCache(); handleCache();
}, []);
useEffect(() => { return () => {
const playPause = async () => {
try {
if (paused) {
await sound.current?.pauseAsync();
EventEmitter.removeListener(PAUSE_AUDIO, pauseSound); EventEmitter.removeListener(PAUSE_AUDIO, pauseSound);
} else { try {
EventEmitter.emit(PAUSE_AUDIO); sound.current?.stopAsync();
EventEmitter.addEventListener(PAUSE_AUDIO, pauseSound);
await Audio.setAudioModeAsync(mode);
await sound.current?.playAsync();
}
} catch { } catch {
// Do nothing // Do nothing
} }
}; };
playPause(); }, []);
useEffect(() => {
if (paused) { if (paused) {
deactivateKeepAwake(); deactivateKeepAwake();
} else { } else {
activateKeepAwake(); activateKeepAwakeAsync();
} }
}, [paused]); }, [paused]);