fix(waveform): fix playback cursor animation using useEffect
This commit is contained in:
@@ -187,15 +187,30 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
||||
}
|
||||
};
|
||||
|
||||
const updatePlaybackPosition = () => {
|
||||
if (!audioContextRef.current || !isPlaying) return;
|
||||
// Animation loop for playback cursor
|
||||
useEffect(() => {
|
||||
if (!isPlaying || !audioContextRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
const elapsed = audioContextRef.current.currentTime - playbackStartTimeRef.current;
|
||||
const currentPos = playbackOffsetRef.current + elapsed;
|
||||
setPlaybackPosition(currentPos);
|
||||
const animate = () => {
|
||||
if (!audioContextRef.current || !isPlaying) return;
|
||||
|
||||
animationFrameRef.current = requestAnimationFrame(updatePlaybackPosition);
|
||||
};
|
||||
const elapsed = audioContextRef.current.currentTime - playbackStartTimeRef.current;
|
||||
const currentPos = playbackOffsetRef.current + elapsed;
|
||||
setPlaybackPosition(currentPos);
|
||||
|
||||
animationFrameRef.current = requestAnimationFrame(animate);
|
||||
};
|
||||
|
||||
animationFrameRef.current = requestAnimationFrame(animate);
|
||||
|
||||
return () => {
|
||||
if (animationFrameRef.current) {
|
||||
cancelAnimationFrame(animationFrameRef.current);
|
||||
}
|
||||
};
|
||||
}, [isPlaying]);
|
||||
|
||||
const handlePlaySegment = (segmentIndex: number) => {
|
||||
if (!audioBuffer || !audioContextRef.current) return;
|
||||
@@ -219,8 +234,6 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
||||
setPlayingSegment(segmentIndex);
|
||||
setPlaybackPosition(segmentStart);
|
||||
|
||||
animationFrameRef.current = requestAnimationFrame(updatePlaybackPosition);
|
||||
|
||||
source.onended = () => {
|
||||
setIsPlaying(false);
|
||||
setPlayingSegment(null);
|
||||
@@ -250,8 +263,6 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
||||
setIsPlaying(true);
|
||||
setPlaybackPosition(startTime);
|
||||
|
||||
animationFrameRef.current = requestAnimationFrame(updatePlaybackPosition);
|
||||
|
||||
source.onended = () => {
|
||||
setIsPlaying(false);
|
||||
setPlaybackPosition(null);
|
||||
|
||||
Reference in New Issue
Block a user