Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c8d1e676c | ||
|
|
37242cdf82 | ||
|
|
d674c4d460 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,6 +13,9 @@ from fastapi.staticfiles import StaticFiles
|
||||
# 导入后端服务
|
||||
from server import app as api_app
|
||||
|
||||
# 确保音频缓存目录存在
|
||||
os.makedirs("audio_cache", exist_ok=True)
|
||||
|
||||
# 主应用
|
||||
app = FastAPI(title="Voice Chat Web")
|
||||
|
||||
@@ -51,6 +54,9 @@ async def proxy_chattts_audio(filename: str):
|
||||
return Response(content=f'{"detail":"{str(e)}"}'.encode(), status_code=500, media_type="application/json")
|
||||
|
||||
|
||||
# 挂载音频文件目录(Edge TTS 生成的 MP3)
|
||||
app.mount("/audio", StaticFiles(directory="audio_cache"), name="audio")
|
||||
|
||||
# 挂载 API
|
||||
app.mount("/api", api_app)
|
||||
|
||||
|
||||
+28
-36
@@ -453,6 +453,9 @@
|
||||
<!-- TTS 设置 -->
|
||||
<div class="tts-section">
|
||||
<h3>🔊 TTS语音合成设置</h3>
|
||||
<div class="browser-tip" id="browserTip" style="display: none; background: #fff3cd; padding: 8px 12px; border-radius: 6px; margin-bottom: 10px; font-size: 13px; color: #856404;">
|
||||
⚠️ 当前浏览器可能限制音频播放,建议使用 Chrome/Firefox,或点击页面任意位置解锁播放
|
||||
</div>
|
||||
<div class="tts-options" id="ttsOptions">
|
||||
<div class="tts-option" data-provider="none">
|
||||
<div class="name">❌ 无 TTS</div>
|
||||
@@ -545,25 +548,16 @@
|
||||
let currentVoice = 'zh-CN-XiaoxiaoNeural';
|
||||
let autoPlay = true; // 自动播放开关
|
||||
let volumeLevel = 1.5; // 音量倍率
|
||||
let audioUnlocked = false; // 音频播放是否解锁
|
||||
let userInteracted = false; // 用户是否已交互
|
||||
|
||||
// 解锁音频播放(浏览器需要用户交互后才能自动播放)
|
||||
function unlockAudio() {
|
||||
if (!audioUnlocked) {
|
||||
// 创建一个静音音频来解锁
|
||||
const silentAudio = new Audio();
|
||||
silentAudio.play().then(() => {
|
||||
audioUnlocked = true;
|
||||
console.log('音频播放已解锁');
|
||||
}).catch(e => {
|
||||
console.log('音频解锁失败:', e);
|
||||
});
|
||||
}
|
||||
}
|
||||
// 用户点击页面解锁音频播放能力
|
||||
document.addEventListener('click', () => {
|
||||
userInteracted = true;
|
||||
}, { once: true });
|
||||
|
||||
// 页面点击解锁音频
|
||||
document.addEventListener('click', unlockAudio, { once: true });
|
||||
document.addEventListener('touchstart', unlockAudio, { once: true });
|
||||
document.addEventListener('touchstart', () => {
|
||||
userInteracted = true;
|
||||
}, { once: true });
|
||||
|
||||
// 元素
|
||||
const statusDot = document.getElementById('statusDot');
|
||||
@@ -588,10 +582,25 @@
|
||||
await checkStatus();
|
||||
await loadTTSProviders();
|
||||
|
||||
// 检测特殊浏览器(小米浏览器等)
|
||||
checkBrowser();
|
||||
|
||||
// 定时检查状态
|
||||
setInterval(checkStatus, 10000);
|
||||
}
|
||||
|
||||
// 检测浏览器兼容性
|
||||
function checkBrowser() {
|
||||
const ua = navigator.userAgent.toLowerCase();
|
||||
const browserTip = document.getElementById('browserTip');
|
||||
|
||||
// 小米浏览器、华为浏览器等国产浏览器UA特征
|
||||
if (ua.includes('miui') || ua.includes('xiaomi') || ua.includes('huawei') ||
|
||||
ua.includes('micromessenger') || ua.includes('quark') || ua.includes('ucbrowser')) {
|
||||
browserTip.style.display = 'block';
|
||||
}
|
||||
}
|
||||
|
||||
// 检查服务状态
|
||||
async function checkStatus() {
|
||||
try {
|
||||
@@ -932,7 +941,7 @@
|
||||
chatSection.scrollTop = chatSection.scrollHeight;
|
||||
|
||||
// 自动播放(在元素添加到DOM后)
|
||||
if (role === 'assistant' && audioData && autoPlay && audioBtnId) {
|
||||
if (role === 'assistant' && audioData && autoPlay && audioBtnId && userInteracted) {
|
||||
setTimeout(() => {
|
||||
const btn = document.getElementById(audioBtnId);
|
||||
if (btn) {
|
||||
@@ -947,9 +956,6 @@
|
||||
const audio = new Audio(url);
|
||||
const icon = btn.querySelector('.play-icon');
|
||||
|
||||
// 应用音量倍率
|
||||
audio.volume = Math.min(volumeLevel, 2); // 最大不超过2
|
||||
|
||||
audio.onplay = () => {
|
||||
icon.textContent = '🔊';
|
||||
btn.classList.add('playing');
|
||||
@@ -960,21 +966,7 @@
|
||||
btn.classList.remove('playing');
|
||||
};
|
||||
|
||||
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('请先点击页面解锁音频播放');
|
||||
}
|
||||
});
|
||||
audio.play();
|
||||
}
|
||||
|
||||
// 显示加载
|
||||
|
||||
Reference in New Issue
Block a user