From eb33ce36885cd2a3737da9e7d17eef53e0905c4e Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Mon, 23 Oct 2023 18:40:05 -0300 Subject: [PATCH] speed playback from array --- app/containers/AudioPlayer/PlaybackSpeed.tsx | 11 ++++++----- app/containers/AudioPlayer/index.tsx | 7 ++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/containers/AudioPlayer/PlaybackSpeed.tsx b/app/containers/AudioPlayer/PlaybackSpeed.tsx index b3cc254b2..2aad1f111 100644 --- a/app/containers/AudioPlayer/PlaybackSpeed.tsx +++ b/app/containers/AudioPlayer/PlaybackSpeed.tsx @@ -4,21 +4,22 @@ import Touchable from 'react-native-platform-touchable'; import styles from './styles'; import { useTheme } from '../../theme'; +import { AVAILABLE_SPEEDS } from './utils'; const PlaybackSpeed = ({ onChange, loaded = false, - rate = 1 + rateIndex = 0 }: { onChange: (value: number) => void; loaded: boolean; - rate: number; + rateIndex: number; }) => { const { colors } = useTheme(); const onPress = () => { - const nextRate = rate === 2 ? 0.5 : rate + 0.5; - onChange(nextRate); + const nextRateIndex = rateIndex >= AVAILABLE_SPEEDS.length ? 0 : rateIndex + 1; + onChange(AVAILABLE_SPEEDS[nextRateIndex]); }; return ( @@ -27,7 +28,7 @@ const PlaybackSpeed = ({ onPress={onPress} style={[styles.containerPlaybackSpeed, { backgroundColor: colors.buttonBackgroundSecondaryDefault }]} > - {rate}x + {AVAILABLE_SPEEDS[rateIndex]}x ); }; diff --git a/app/containers/AudioPlayer/index.tsx b/app/containers/AudioPlayer/index.tsx index 0837a1bc4..ef3a75597 100644 --- a/app/containers/AudioPlayer/index.tsx +++ b/app/containers/AudioPlayer/index.tsx @@ -11,6 +11,7 @@ import Seek from './Seek'; import PlaybackSpeed from './PlaybackSpeed'; import PlayButton from './PlayButton'; import audioPlayer from '../../lib/methods/audioPlayer'; +import { AVAILABLE_SPEEDS } from './utils'; interface IAudioPlayerProps { fileUri: string; @@ -28,7 +29,7 @@ const AudioPlayer = ({ onPressCallback = () => {} }: IAudioPlayerProps) => { const [paused, setPaused] = useState(true); - const [rate, setRate] = useState(1); + const [rateIndex, setRateIndex] = useState(0); const duration = useSharedValue(0); const currentTime = useSharedValue(0); @@ -63,7 +64,7 @@ const AudioPlayer = ({ if (currentSecond <= durationSeconds) { currentTime.value = currentSecond; } - setRate(data.rate); + setRateIndex(AVAILABLE_SPEEDS.indexOf(data.rate)); } }; @@ -142,7 +143,7 @@ const AudioPlayer = ({ - + );