react-voice-visualizer icon indicating copy to clipboard operation
react-voice-visualizer copied to clipboard

Error in setCurrentAudioTime

Open MiteshDarda opened this issue 1 year ago • 1 comments

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:

  1. Play the recording let jump to any second expect 0
  2. Use the function recorderControls.setCurrentAudioTime(0);
  3. Then call the funciton recorderControls.togglePauseResume()
  4. See the error that time is not updated properly

Expected behavior When i do togglePauseResume it should play from 0 seconds

** Screenshots** Watch the video

** Package info ** 2.0.2

** Additional context **

const restartPlayback = () => {
     recorderControls.setCurrentAudioTime(0);
     recorderControls.togglePauseResume()
}

MiteshDarda avatar Jul 23 '24 08:07 MiteshDarda

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.

justinformentin avatar Jul 26 '24 15:07 justinformentin