fix(waveform): fix playback cursor animation using useEffect

This commit is contained in:
Hördle Bot
2025-11-23 01:10:04 +01:00
parent e06e0d2919
commit 54f47a9470

View File

@@ -187,16 +187,31 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
}
};
const updatePlaybackPosition = () => {
// Animation loop for playback cursor
useEffect(() => {
if (!isPlaying || !audioContextRef.current) {
return;
}
const animate = () => {
if (!audioContextRef.current || !isPlaying) return;
const elapsed = audioContextRef.current.currentTime - playbackStartTimeRef.current;
const currentPos = playbackOffsetRef.current + elapsed;
setPlaybackPosition(currentPos);
animationFrameRef.current = requestAnimationFrame(updatePlaybackPosition);
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);