From 81fd471fd91ef3d5679bcf2a574870b63c397aed Mon Sep 17 00:00:00 2001 From: wuxichen <17301714657@163.com> Date: Fri, 17 Oct 2025 10:01:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Daudio=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../translate/component/message/index.tsx | 118 ++++++------------ 1 file changed, 38 insertions(+), 80 deletions(-) diff --git a/projects/translate-h5/src/view/home/translate/component/message/index.tsx b/projects/translate-h5/src/view/home/translate/component/message/index.tsx index b10ae53..05a66b0 100644 --- a/projects/translate-h5/src/view/home/translate/component/message/index.tsx +++ b/projects/translate-h5/src/view/home/translate/component/message/index.tsx @@ -17,92 +17,50 @@ function Index(props: DefinedProps) { const [isPlaying, setIsPlating] = useState(false); const [currentPlayingId, setCurrentPlayingId] = useState(); - useEffect(() => { - if (isRecording) { - stopAllAudio(); - } - }, [isRecording]); - const onVoiceChange = () => { - setIsPlating(!isPlaying); - }; + // useEffect(() => { + // if (isRecording) { + // stopAllAudio(); + // } + // }, [isRecording]); + const playAudio = (id: number, audioUrl: string) => { if (isRecording) { Toast.show("录音中,无法播放音频"); return; } - - if (currentPlayingId === id && audioRefs.current[id]) { - if (audioRefs.current[id]) { - audioRefs.current[id].pause(); - audioRefs.current[id].currentTime = 0; - } - setCurrentPlayingId(undefined); - setIsPlating(false); - return; - } - - stopAllAudio(); - if (!audioRefs.current[id]) { - audioRefs.current[id] = new Audio(audioUrl); - } - - const audio = audioRefs.current[id]; - audio.currentTime = 0; - - audio.onended = () => { - setCurrentPlayingId(undefined); - setIsPlating(false); - }; - - audio.onerror = () => { - const error = audio.error; - switch (error?.code) { - case MediaError.MEDIA_ERR_ABORTED: - console.warn("音频播放被用户中断"); - break; - case MediaError.MEDIA_ERR_NETWORK: - console.warn("网络错误,无法加载音频"); - break; - case MediaError.MEDIA_ERR_DECODE: - console.warn("解码失败,可能是格式不支持"); - break; - case MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED: - console.warn("浏览器不支持该音频格式"); - break; - default: - console.warn("未知错误"); - } - Toast.show("音频播放失败1111"); - setIsPlating(false); - }; - console.log("audio play", id); - audio - .play() - .then(() => { - setCurrentPlayingId(id); - setIsPlating(true); - }) - .catch((error) => { - console.error("音频播放失败:", error); - Toast.show("音频播放失败"); - }); - }; - const stopAllAudio = () => { if (currentPlayingId && audioRefs.current[currentPlayingId]) { audioRefs.current[currentPlayingId].pause(); audioRefs.current[currentPlayingId].currentTime = 0; - setIsPlating(false); - setCurrentPlayingId(undefined); - return; } - - Object.values(audioRefs.current).forEach((audio) => { - if (!audio.paused) { - audio.pause(); - audio.currentTime = 0; - } - }); + if (currentPlayingId !== id) { + setCurrentPlayingId(id); + audioRefs.current[id] = new Audio(audioUrl); + audioRefs.current[id].play(); + audioRefs.current[id].onended = () => { + setCurrentPlayingId(undefined); + }; + } else { + audioRefs.current[id].pause(); + audioRefs.current[id].currentTime = 0; + setCurrentPlayingId(undefined); + } }; + // const stopAllAudio = () => { + // if (currentPlayingId && audioRefs.current[currentPlayingId]) { + // audioRefs.current[currentPlayingId].pause(); + // audioRefs.current[currentPlayingId].currentTime = 0; + // setIsPlating(false); + // setCurrentPlayingId(undefined); + // return; + // } + + // Object.values(audioRefs.current).forEach((audio) => { + // if (!audio.paused) { + // audio.pause(); + // audio.currentTime = 0; + // } + // }); + // }; const renderAvatar = (item: Message) => { return ; }; @@ -155,7 +113,7 @@ function Index(props: DefinedProps) { return (
{data.map((item, index) => ( -
playAudio(item.id, item.contentText)}> +
{renderAvatar(item)}
@@ -166,10 +124,10 @@ function Index(props: DefinedProps) { {item.createTime}
-
+
playAudio(item.id, item.contentText)}>
{item.contentDuration}''