fix: 修复自动播放时机问题,audioBtnId作用域修复

This commit is contained in:
2026-04-22 18:38:30 +08:00
parent 7778522c96
commit 0a51190482
2 changed files with 12 additions and 11 deletions

Binary file not shown.

View File

@@ -879,6 +879,8 @@
const msg = document.createElement('div');
msg.className = `message ${role}`;
let audioBtnId = ''; // 在函数顶部声明
if (role === 'user' && content instanceof Blob) {
const audioUrl = URL.createObjectURL(content);
msg.innerHTML = `
@@ -892,7 +894,6 @@
`;
} else if (role === 'assistant') {
let audioHtml = '';
let audioBtnId = '';
if (audioData) {
audioBtnId = `audioBtn_${Date.now()}`;
audioHtml = `<button class="play-btn tts-play-btn" id="${audioBtnId}" onclick="playAudio('${audioData}', this)">
@@ -904,9 +905,15 @@
<div class="role">AI</div>
<div class="content">${content}${audioHtml}</div>
`;
} else {
msg.innerHTML = `<div class="role">我</div><div class="content">${content}</div>`;
}
// 自动播放(如果开启)
if (audioData && autoPlay) {
chatSection.appendChild(msg);
chatSection.scrollTop = chatSection.scrollHeight;
// 自动播放在元素添加到DOM后
if (role === 'assistant' && audioData && autoPlay && audioBtnId) {
setTimeout(() => {
const btn = document.getElementById(audioBtnId);
if (btn) {
@@ -914,12 +921,6 @@
}
}, 100);
}
} else {
msg.innerHTML = `<div class="role">我</div><div class="content">${content}</div>`;
}
chatSection.appendChild(msg);
chatSection.scrollTop = chatSection.scrollHeight;
}
// 播放音频