diff --git a/src/components/audio-animation/index.tsx b/src/components/audio-animation/index.tsx index f24c3f3a..2a297c68 100644 --- a/src/components/audio-animation/index.tsx +++ b/src/components/audio-animation/index.tsx @@ -189,4 +189,4 @@ const AudioAnimation: React.FC = (props) => { ); }; -export default React.memo(AudioAnimation); +export default AudioAnimation; diff --git a/src/components/audio-player/raw-audio-player.tsx b/src/components/audio-player/raw-audio-player.tsx index 10fea230..69a1fa76 100644 --- a/src/components/audio-player/raw-audio-player.tsx +++ b/src/components/audio-player/raw-audio-player.tsx @@ -18,8 +18,9 @@ const RawAudioPlayer: React.FC = forwardRef((props, ref) => { // ======================================================== const initAudioContext = useCallback(() => { - audioContext.current = new (window.AudioContext || - window.webkitAudioContext)(); + audioContext.current = new ( + window.AudioContext || (window as any).webkitAudioContext + )(); analyser.current = audioContext.current.createAnalyser(); analyser.current.fftSize = 512; @@ -52,7 +53,9 @@ const RawAudioPlayer: React.FC = forwardRef((props, ref) => { }); audioRef.current.addEventListener('timeupdate', () => { + const current = audioRef.current?.currentTime || 0; props.onTimeUpdate?.(); + props.onAudioProcess?.(current); }); audioRef.current.addEventListener('ended', () => { @@ -85,11 +88,6 @@ const RawAudioPlayer: React.FC = forwardRef((props, ref) => { props.onVolumeChange?.(); }); - audioRef.current.addEventListener('audioprocess', () => { - const current = audioRef.current?.currentTime || 0; - props.onAudioProcess?.(current); - }); - audioRef.current.addEventListener('playing', () => { props.onPlaying?.(); }); @@ -111,16 +109,44 @@ const RawAudioPlayer: React.FC = forwardRef((props, ref) => { useImperativeHandle(ref, () => ({ play: () => { - audioRef.current?.play(); + if (audioRef.current) { + // If playback has ended, reset to beginning + if (audioRef.current.currentTime >= audioRef.current.duration) { + audioRef.current.currentTime = 0; + } + audioRef.current.play(); + } }, pause: () => { audioRef.current?.pause(); + }, + seekTo: (ratio: number) => { + if (audioRef.current && audioRef.current.duration) { + audioRef.current.currentTime = ratio * audioRef.current.duration; + } + }, + // Compatibility layer for wavesurfer interface + wavesurfer: { + current: { + play: () => { + if (audioRef.current) { + // If playback has ended, reset to beginning + if (audioRef.current.currentTime >= audioRef.current.duration) { + audioRef.current.currentTime = 0; + } + return audioRef.current.play(); + } + return Promise.resolve(); + }, + isPlaying: () => { + return audioRef.current ? !audioRef.current?.paused : false; + } + } } })); useEffect(() => { if (audioRef.current) { - console.log('audioRef.current', audioRef.current, props.url); initEnvents(); } return () => { @@ -138,7 +164,6 @@ const RawAudioPlayer: React.FC = forwardRef((props, ref) => { audioRef.current?.removeEventListener('seeked', () => {}); audioRef.current?.removeEventListener('seeking', () => {}); audioRef.current?.removeEventListener('volumechange', () => {}); - audioRef.current?.removeEventListener('audioprocess', () => {}); audioRef.current?.removeEventListener('playing', () => {}); audioRef.current?.removeEventListener('loadedmetadata', () => {}); audioRef.current?.removeEventListener('ended', () => {}); @@ -146,6 +171,13 @@ const RawAudioPlayer: React.FC = forwardRef((props, ref) => { }; }, [audioRef.current]); + // Reload audio when URL changes + useEffect(() => { + if (audioRef.current && props.url) { + audioRef.current.load(); + } + }, [props.url]); + return (