添加文本朗读器

This commit is contained in:
2025-10-07 19:32:00 +08:00
parent 67ab729d15
commit b4d62ef111
19 changed files with 272 additions and 145 deletions

View File

@@ -5,36 +5,30 @@ export function useAudioPlayer() {
const audioRef = useRef<HTMLAudioElement | null>(null);
useEffect(() => {
audioRef.current = new Audio();
return () => {
if (audioRef.current) {
audioRef.current.pause();
audioRef.current = null;
}
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 playAudio = async (audioUrl: string) => {
audioRef.current!.src = audioUrl;
try {
await audioRef.current!.play();
} catch (e) {
return e;
}
};
const pauseAudio = () => {
if (audioRef.current) {
audioRef.current.pause();
}
audioRef.current!.pause();
};
const stopAudio = () => {
if (audioRef.current) {
audioRef.current.pause();
audioRef.current.currentTime = 0;
}
audioRef.current!.pause();
audioRef.current!.currentTime = 0;
};
return {
playAudio,
pauseAudio,
stopAudio
stopAudio,
audioRef
};
}