添加文本朗读器
This commit is contained in:
@@ -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
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user