From 2bcaadbeb64d8a326a5907831ec6f6f2041d8acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B6=85=E7=BA=A7=E8=80=81=E7=99=BD=E5=85=94?= Date: Wed, 10 Sep 2025 11:52:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=9F=B3=E9=A2=91=E6=B6=88=E6=81=AF):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=9F=B3=E9=A2=91URL=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E9=81=BF=E5=85=8DCORS=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改音频URL检测逻辑,对于阿里云OSS等外部资源直接返回true,避免fetch HEAD请求触发CORS问题。同时调整crossOrigin设置策略,仅在需要访问音频数据时设置该属性。移除了冗余的备用播放方案,简化错误处理流程。 --- .../components/AudioMessage/AudioMessage.tsx | 55 +++++++------------ 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/Cunkebao/src/pages/pc/ckbox/weChat/components/ChatWindow/components/MessageRecord/components/AudioMessage/AudioMessage.tsx b/Cunkebao/src/pages/pc/ckbox/weChat/components/ChatWindow/components/MessageRecord/components/AudioMessage/AudioMessage.tsx index 0d2ba39b..f087165c 100644 --- a/Cunkebao/src/pages/pc/ckbox/weChat/components/ChatWindow/components/MessageRecord/components/AudioMessage/AudioMessage.tsx +++ b/Cunkebao/src/pages/pc/ckbox/weChat/components/ChatWindow/components/MessageRecord/components/AudioMessage/AudioMessage.tsx @@ -35,13 +35,20 @@ const parseAudioUrl = (audioUrl: string): AudioData => { }; }; -// 测试音频URL是否可访问 +// 测试音频URL是否可访问(避免CORS问题) const testAudioUrl = async (url: string): Promise => { try { + // 对于阿里云OSS等外部资源,直接返回true,让Audio对象自己处理 + // 避免fetch HEAD请求触发CORS问题 + if (url.includes(".aliyuncs.com") || url.includes("oss-")) { + return true; + } + const response = await fetch(url, { method: "HEAD" }); return response.ok; } catch (error) { - return false; + // 如果fetch失败(可能是CORS问题),返回true让Audio对象尝试加载 + return true; } }; @@ -88,8 +95,14 @@ const AudioMessage: React.FC = ({ audioUrl, msgId }) => { const newAudio = new Audio(); - // 设置跨域属性 - newAudio.crossOrigin = "anonymous"; + // 对于阿里云OSS等外部资源,不设置crossOrigin避免CORS问题 + // 只有在需要访问音频数据时才设置crossOrigin + if ( + !actualAudioUrl.includes(".aliyuncs.com") && + !actualAudioUrl.includes("oss-") + ) { + newAudio.crossOrigin = "anonymous"; + } newAudio.preload = "metadata"; audioRefs.current[audioId] = newAudio; @@ -120,37 +133,9 @@ const AudioMessage: React.FC = ({ audioUrl, msgId }) => { await newAudio.play(); setPlayingAudioId(audioId); } catch (error) { - // 尝试备用方案:不设置crossOrigin - try { - const fallbackAudio = new Audio(actualAudioUrl); - audioRefs.current[audioId] = fallbackAudio; - - // 重新绑定事件监听器 - fallbackAudio.addEventListener("timeupdate", () => { - const currentProgress = - (fallbackAudio.currentTime / fallbackAudio.duration) * 100; - setAudioProgress(prev => ({ - ...prev, - [audioId]: currentProgress, - })); - }); - - fallbackAudio.addEventListener("ended", () => { - setPlayingAudioId(null); - setAudioProgress(prev => ({ ...prev, [audioId]: 0 })); - }); - - fallbackAudio.addEventListener("error", e => { - setPlayingAudioId(null); - setAudioError("音频播放失败,请稍后重试"); - }); - - await fallbackAudio.play(); - setPlayingAudioId(audioId); - } catch (fallbackError) { - setPlayingAudioId(null); - setAudioError("音频播放失败,请检查音频格式或网络连接"); - } + setPlayingAudioId(null); + setAudioError("音频播放失败,请检查音频格式或网络连接"); + console.error("音频播放错误:", error); } } else { if (isPlaying) {