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