新增全语言翻译器
This commit is contained in:
40
src/hooks/useAudioPlayer.ts
Normal file
40
src/hooks/useAudioPlayer.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { useRef, useEffect } from "react";
|
||||
|
||||
|
||||
export function useAudioPlayer() {
|
||||
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||
useEffect(() => {
|
||||
audioRef.current = new Audio();
|
||||
|
||||
return () => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
audioRef.current = null;
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
const playAudio = (audioUrl: string) => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.src = audioUrl;
|
||||
audioRef.current.play().catch(error => {
|
||||
console.error('播放失败:', error);
|
||||
});
|
||||
}
|
||||
};
|
||||
const pauseAudio = () => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
}
|
||||
};
|
||||
const stopAudio = () => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
audioRef.current.currentTime = 0;
|
||||
}
|
||||
};
|
||||
return {
|
||||
playAudio,
|
||||
pauseAudio,
|
||||
stopAudio
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user