fix rate state

This commit is contained in:
Reinaldo Neto 2023-08-16 18:16:19 -03:00
parent b88c3b1386
commit 14625821d6
2 changed files with 6 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import React, { useState } from 'react'; import React from 'react';
import { Text } from 'react-native'; import { Text } from 'react-native';
import styles from './styles'; import styles from './styles';
@ -8,18 +8,16 @@ import Touchable from '../../Touchable';
const AudioRate = ({ const AudioRate = ({
onChange, onChange,
loaded = false, loaded = false,
rate: rateProps = 1 rate = 1
}: { }: {
onChange: (value: number) => void; onChange: (value: number) => void;
loaded: boolean; loaded: boolean;
rate: number; rate: number;
}) => { }) => {
const [rate, setRate] = useState(rateProps);
const { colors } = useTheme(); const { colors } = useTheme();
const onPress = () => { const onPress = () => {
const nextRate = rate === 2 ? 0.5 : rate + 0.5; const nextRate = rate === 2 ? 0.5 : rate + 0.5;
setRate(nextRate);
onChange(nextRate); onChange(nextRate);
}; };

View File

@ -29,6 +29,7 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [paused, setPaused] = useState(true); const [paused, setPaused] = useState(true);
const [cached, setCached] = useState(false); const [cached, setCached] = useState(false);
const [rate, setRate] = useState(1);
const duration = useSharedValue(0); const duration = useSharedValue(0);
const currentTime = useSharedValue(0); const currentTime = useSharedValue(0);
@ -37,7 +38,6 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
const { colors } = useTheme(); const { colors } = useTheme();
const audioUri = useRef<string>(''); const audioUri = useRef<string>('');
const rate = useRef<number>(1);
const onPlaybackStatusUpdate = (status: AVPlaybackStatus) => { const onPlaybackStatusUpdate = (status: AVPlaybackStatus) => {
if (status) { if (status) {
@ -66,7 +66,7 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
if (data.isLoaded && data.durationMillis) { if (data.isLoaded && data.durationMillis) {
const durationSeconds = data.durationMillis / 1000; const durationSeconds = data.durationMillis / 1000;
duration.value = durationSeconds > 0 ? durationSeconds : 0; duration.value = durationSeconds > 0 ? durationSeconds : 0;
rate.current = data.rate; setRate(data.rate);
} }
}; };
@ -116,7 +116,7 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
} }
}; };
const setRate = async (value = 1.0) => { const onChangeRate = async (value = 1.0) => {
await handleAudioMedia.setRateAsync(audioUri.current, value); await handleAudioMedia.setRateAsync(audioUri.current, value);
}; };
@ -213,7 +213,7 @@ const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessage
> >
<PlayButton disabled={isReply} loading={loading} paused={paused} cached={cached} onPress={onPress} /> <PlayButton disabled={isReply} loading={loading} paused={paused} cached={cached} onPress={onPress} />
<Slider currentTime={currentTime} duration={duration} loaded={!isReply && cached} onChangeTime={setPosition} /> <Slider currentTime={currentTime} duration={duration} loaded={!isReply && cached} onChangeTime={setPosition} />
<AudioRate onChange={setRate} loaded={!isReply && cached} rate={rate.current} /> <AudioRate onChange={onChangeRate} loaded={!isReply && cached} rate={rate} />
</View> </View>
</> </>
); );