优化播放
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
Song367 2025-10-21 11:10:33 +08:00
parent 0c410b9f57
commit 8fb1e3c963

View File

@ -56,56 +56,43 @@ let isFirstChunk = true;
// 队列处理器 - 独立运行,按顺序播放音频 // 队列处理器 - 独立运行,按顺序播放音频
async function processAudioQueue() { async function processAudioQueue() {
if (isProcessingQueue) return; if (isProcessingQueue) return;
isProcessingQueue = true; isProcessingQueue = true;
while (audioQueue.length > 0 && !isPlaying) { while (audioQueue.length > 0 && !isPlaying) {
console.log('开始处理音频队列'); console.log('开始处理音频队列');
// 如果当前没有音频在播放,且队列中有音频
if (!isPlaying && audioQueue.length > 0) { if (!isPlaying && audioQueue.length > 0) {
const audioItem = audioQueue.shift(); const audioItem = audioQueue.shift();
const sayName = '8-4-sh' const sayName = '8-4-sh';
const targetVideo = window.webrtcApp.interactionVideo const targetVideo = window.webrtcApp.interactionVideo;
// 如果是第一个音频片段,触发视频切换
if (sayName != window.webrtcApp.currentVideoTag && window.webrtcApp && window.webrtcApp.switchVideoStream) { if (sayName != window.webrtcApp.currentVideoTag && window.webrtcApp && window.webrtcApp.switchVideoStream) {
try { try {
// 检查WebSocket连接状态 // 检查WebSocket连接状态(仅影响服务端广播,不阻断本地播放)
if (window.webrtcApp.checkConnectionStatus && !window.webrtcApp.checkConnectionStatus()) { if (window.webrtcApp.checkConnectionStatus && !window.webrtcApp.checkConnectionStatus()) {
console.log('WebSocket连接异常跳过视频切换'); console.log('WebSocket连接异常继续本地播放并切换视频');
return; } else {
}
console.log('--------------触发视频切换:', sayName); console.log('--------------触发视频切换:', sayName);
window.webrtcApp.switchVideoStream(targetVideo, 'audio', '8-4-sh'); window.webrtcApp.switchVideoStream(targetVideo, 'audio', '8-4-sh');
}
isFirstChunk = false; isFirstChunk = false;
window.webrtcApp.currentVideoTag = sayName; window.webrtcApp.currentVideoTag = sayName;
} catch (error) { } catch (error) {
console.error('视频切换失败:', error); console.error('视频切换失败:', error);
} }
} }
await playAudioData(audioItem.audioData); await playAudioData(audioItem.audioData);
} else { } else {
// 等待一小段时间再检查
await new Promise(resolve => setTimeout(resolve, 50)); await new Promise(resolve => setTimeout(resolve, 50));
} }
} }
isProcessingQueue = false; isProcessingQueue = false;
const text = 'default';
// 等待当前音频播放完成后再切换回默认视频 console.log("音频结束------------------------", window.webrtcApp.currentVideoTag, isPlaying);
// while (isPlaying) {
// console.log("触发音频等待")
// await new Promise(resolve => setTimeout(resolve, 1000));
// }
// console.log("触发音频等待")
// await new Promise(resolve => setTimeout(resolve, 300));
const text = 'default'
console.log("音频结束------------------------", window.webrtcApp.currentVideoTag, isPlaying)
if (window.webrtcApp.currentVideoTag != text && !isPlaying) { if (window.webrtcApp.currentVideoTag != text && !isPlaying) {
isFirstChunk = true isFirstChunk = true;
window.webrtcApp.currentVideoTag = text window.webrtcApp.currentVideoTag = text;
window.webrtcApp.switchVideoStream(window.webrtcApp.defaultVideo, 'audio', text); window.webrtcApp.switchVideoStream(window.webrtcApp.defaultVideo, 'audio', text);
} }
console.log('音频队列处理完成'); console.log('音频队列处理完成');