react-voice-visualizer
react-voice-visualizer copied to clipboard
Error in setCurrentAudioTime
Describe the bug setCurrentAudioTime is taking the indicator correctly to the given time, but when i use togglePauseResume() it starts from the old time stamp, i think setCurrentAudioTime it is not updating
To Reproduce Steps to reproduce the behavior:
- Play the recording let jump to any second expect 0
- Use the function recorderControls.setCurrentAudioTime(0);
- Then call the funciton recorderControls.togglePauseResume()
- See the error that time is not updated properly
Expected behavior When i do togglePauseResume it should play from 0 seconds
** Screenshots**
** Package info ** 2.0.2
** Additional context **
const restartPlayback = () => {
recorderControls.setCurrentAudioTime(0);
recorderControls.togglePauseResume()
}
I had the same problem. Looks like the issue is here https://github.com/YZarytskyi/react-voice-visualizer/blob/c328cb2b64683c356b4603f9cf77ac0cb3143ee3/src/hooks/useVoiceVisualizer.tsx#L204
It seems that setting setCurrentAudioTime doesn't have an effect on anything because currentAudioTime gets immediately overwritten by audioRef.current.currentTime in the requestAnimationFrame loop.
We just need a way to change the audioRef.current.currentTime @YZarytskyi let me know your thoughts:
Keep setCurrentAudioTime and audioRef.current internal, and expose a a new function, something like
const overrideCurrentAudioTime = (time: number): void => {
if (!audioRef.current) return;
audioRef.current.currentTime = time;
setCurrentAudioTime(audioRef.current.currentTime);
}
Where the audioRef.current.currentTime gets set so it plays from that time, and the currentAudioTime state gets set which is necessary to move the indicator if the track is paused.