Skip to content

Commit

Permalink
chore: in list + hidden stop button + extract wave form on play
Browse files Browse the repository at this point in the history
  • Loading branch information
dprevost-LMI committed Nov 9, 2024
1 parent 891a7c5 commit 97cf9f8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
46 changes: 30 additions & 16 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ const RenderListItem = React.memo(
onError={error => {
console.log('Error in static player:', error);
}}
onCurrentProgressChange={(currentProgress, songDuration) => {
console.log(
`currentProgress ${currentProgress}, songDuration ${songDuration}`
);
onCurrentProgressChange={(_currentProgress, _songDuration) => {
// console.log(
// `currentProgress ${currentProgress}, songDuration ${songDuration}`
// );
}}
onChangeWaveformLoadState={state => {
setIsLoading(state);
Expand Down Expand Up @@ -225,6 +225,8 @@ const AppContainer = () => {
const [list, setList] = useState<ListItem[]>([]);
const [currentPlaybackSpeed, setCurrentPlaybackSpeed] =
useState<PlaybackSpeedType>(1.0);
const [showAdvancedOptions, setShowAdvancedOptions] =
useState<boolean>(false);

const { top, bottom } = useSafeAreaInsets();
const styles = stylesheet({ top, bottom });
Expand All @@ -247,6 +249,10 @@ const AppContainer = () => {
);
};

const toggleAdvancedOptions = () => {
setShowAdvancedOptions(!showAdvancedOptions);
};

const handleStopPlayersAndExtractors = async () => {
const { stopPlayersAndExtractors } = useAudioPlayer();
const hasStoppedAll: boolean[] = await stopPlayersAndExtractors();
Expand Down Expand Up @@ -277,23 +283,31 @@ const AppContainer = () => {
<GestureHandlerRootView style={styles.appContainer}>
<View style={styles.screenBackground}>
<View style={styles.container}>
<View style={styles.simformImageContainer}>
<Pressable
style={styles.simformImageContainer}
onPress={toggleAdvancedOptions}>
<Image
source={Icons.simform}
style={styles.simformImage}
resizeMode="contain"
/>
<Pressable
style={styles.playBackControlPressable}
onPress={handleStopPlayersAndExtractors}>
<FastImage
source={Icons.stop}
style={styles.stopAllButton}
resizeMode="contain"
tintColor={Colors.pink}
/>
</Pressable>
</View>
</Pressable>
{showAdvancedOptions && (
<>
<Pressable
style={styles.stopAllRecordingContainer}
onPress={handleStopPlayersAndExtractors}>
<Image
source={Icons.stop}
style={styles.pinkButtonImage}
resizeMode="contain"
/>
<Text style={styles.stopAllRecordingTitle}>
{'Stop all players and extractors'}
</Text>
</Pressable>
</>
)}
<ScrollView scrollEnabled={shouldScroll}>
{list.map(item => (
<RenderListItem
Expand Down
13 changes: 12 additions & 1 deletion example/src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const styles = (params: StyleSheetParams = {}) =>
tintColor: Colors.white,
alignSelf: 'flex-end',
},
stopAllButton: {
pinkButtonImage: {
height: scale(22),
width: scale(22),
tintColor: Colors.pink,
Expand Down Expand Up @@ -101,6 +101,17 @@ const styles = (params: StyleSheetParams = {}) =>
justifyContent: 'space-around',
flexDirection: 'row',
},
stopAllRecordingContainer: {
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'row',
},
stopAllRecordingTitle: {
fontSize: scale(20),
fontWeight: 'bold',
color: Colors.pink,
paddingLeft: scale(8),
},
loadingText: {
color: Colors.black,
},
Expand Down
8 changes: 7 additions & 1 deletion src/components/Waveform/Waveform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
const [panMoving, setPanMoving] = useState(false);
const [playerState, setPlayerState] = useState(PlayerState.stopped);
const [recorderState, setRecorderState] = useState(RecorderState.stopped);
const [isWaveformExtracted, setWaveformExtracted] = useState(false);
const audioSpeed: number =
playbackSpeed > playbackSpeedThreshold ? 1.0 : playbackSpeed;

Expand Down Expand Up @@ -188,6 +189,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
if (!isNil(waveforms) && !isEmpty(waveforms)) {
setWaveform(waveforms);
await preparePlayerAndGetDuration();
setWaveformExtracted(true);
}
}
} catch (err) {
Expand Down Expand Up @@ -230,7 +232,11 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
if (mode === 'static') {
try {
if (playerState === PlayerState.stopped) {
await preparePlayerForPath(currentProgress);
if (isWaveformExtracted) {
await preparePlayerForPath(currentProgress);
} else {
await getAudioWaveFormForPath(noOfSamples);
}
}

const play = await playPlayer({
Expand Down

0 comments on commit 97cf9f8

Please sign in to comment.