fix: 修复ChatTTS音频代理路由和音频播放错误处理

This commit is contained in:
2026-04-22 21:57:32 +08:00
parent c93b83e2cf
commit d0fc1f8cff
3 changed files with 63 additions and 3 deletions

View File

@@ -545,6 +545,25 @@
let currentVoice = 'zh-CN-XiaoxiaoNeural';
let autoPlay = true; // 自动播放开关
let volumeLevel = 1.5; // 音量倍率
let audioUnlocked = false; // 音频播放是否解锁
// 解锁音频播放(浏览器需要用户交互后才能自动播放)
function unlockAudio() {
if (!audioUnlocked) {
// 创建一个静音音频来解锁
const silentAudio = new Audio();
silentAudio.play().then(() => {
audioUnlocked = true;
console.log('音频播放已解锁');
}).catch(e => {
console.log('音频解锁失败:', e);
});
}
}
// 页面点击解锁音频
document.addEventListener('click', unlockAudio, { once: true });
document.addEventListener('touchstart', unlockAudio, { once: true });
// 元素
const statusDot = document.getElementById('statusDot');
@@ -937,11 +956,25 @@
};
audio.onended = () => {
icon.textContent = url.startsWith('/audio') || url.startsWith('http') ? '🔊' : '▶️';
icon.textContent = url.startsWith('/audio') || url.startsWith('http') || url.startsWith('blob:') ? '🔊' : '▶️';
btn.classList.remove('playing');
};
audio.play();
audio.onerror = (e) => {
console.error('音频播放失败:', url, e);
icon.textContent = '❌';
btn.classList.remove('playing');
};
// 添加播放失败的catch
audio.play().catch(e => {
console.error('播放被阻止:', e);
icon.textContent = '🔇';
// 提示用户点击播放
if (e.name === 'NotAllowedError') {
showError('请先点击页面解锁音频播放');
}
});
}
// 显示加载