fix togglePlayPause
This commit is contained in:
parent
29b61e0ef3
commit
c469d0f8e9
|
@ -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 () => {
|
EventEmitter.removeListener(PAUSE_AUDIO, pauseSound);
|
||||||
try {
|
try {
|
||||||
if (paused) {
|
sound.current?.stopAsync();
|
||||||
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 {
|
} catch {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
playPause();
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
if (paused) {
|
if (paused) {
|
||||||
deactivateKeepAwake();
|
deactivateKeepAwake();
|
||||||
} else {
|
} else {
|
||||||
activateKeepAwake();
|
activateKeepAwakeAsync();
|
||||||
}
|
}
|
||||||
}, [paused]);
|
}, [paused]);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue