Skip to content

Commit 2f0b576

Browse files
committed
fix: reconcile code with the pause/resume feature
1 parent 686e71f commit 2f0b576

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

example/src/App.tsx

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,41 @@ const RenderListItem = React.memo(
6363
const [isLoading, setIsLoading] = useState(true);
6464

6565
const handlePlayPauseAction = async () => {
66-
let currentPlayer = currentPlayingRef?.current;
67-
68-
// If no player or if current player is stopped just start it!
66+
// If we are recording do nothing
6967
if (
70-
currentPlayer == null ||
71-
currentPlayer.currentState === PlayerState.stopped
68+
currentPlayingRef?.current?.currentState === RecorderState.recording
7269
) {
70+
return;
71+
}
72+
73+
const startNewPlayer = async () => {
7374
currentPlayingRef = ref;
74-
await currentPlayingRef.current?.startPlayer({
75-
finishMode: FinishMode.stop,
76-
});
77-
} else {
78-
// If we are recording do nothing
79-
if (currentPlayer.currentState === RecorderState.recording) {
80-
return;
75+
if (ref.current?.currentState === PlayerState.paused) {
76+
await ref.current?.resumePlayer();
77+
} else {
78+
await ref.current?.startPlayer({
79+
finishMode: FinishMode.stop,
80+
});
8181
}
82+
};
8283

84+
// If no player or if current player is stopped just start the new player!
85+
if (
86+
currentPlayingRef == null ||
87+
[PlayerState.stopped, PlayerState.paused].includes(
88+
currentPlayingRef?.current?.currentState as PlayerState
89+
)
90+
) {
91+
await startNewPlayer();
92+
} else {
8393
// Pause current player if it was playing
84-
if (currentPlayer.currentState === PlayerState.playing) {
94+
if (currentPlayingRef?.current?.currentState === PlayerState.playing) {
8595
await currentPlayingRef?.current?.pausePlayer();
8696
}
8797

8898
// Start player when it is a different one!
89-
if (currentPlayer.playerKey() !== ref?.current?.playerKey()) {
90-
currentPlayingRef = ref;
91-
await currentPlayingRef.current?.startPlayer({
92-
finishMode: FinishMode.stop,
93-
});
99+
if (currentPlayingRef?.current?.playerKey !== ref?.current?.playerKey) {
100+
await startNewPlayer();
94101
}
95102
}
96103
};

src/components/Waveform/Waveform.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
587587
stopRecord: stopRecordingAction,
588588
resumeRecord: resumeRecordingAction,
589589
currentState: mode === 'static' ? playerState : recorderState,
590-
playerKey: () => path,
590+
playerKey: path,
591591
}));
592592

593593
return (

src/components/Waveform/WaveformTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ export interface IWaveformRef {
5252
pauseRecord: () => Promise<boolean>;
5353
resumeRecord: () => Promise<boolean>;
5454
currentState: PlayerState | RecorderState;
55-
playerKey: () => string;
55+
playerKey: string;
5656
}

0 commit comments

Comments
 (0)