处理结尾判断

This commit is contained in:
宋居成 2025-07-29 01:36:40 +08:00
parent a27112d6cb
commit a96fc86d42
2 changed files with 77 additions and 13 deletions

View File

@ -390,6 +390,70 @@ class WebRTCChat {
}); });
this.logMessage(`视频流创建成功: ${videoFile}`, 'success'); this.logMessage(`视频流创建成功: ${videoFile}`, 'success');
if (videoFile === this.defaultVideo) {
let lastCurrentTime = 0;
video.addEventListener('timeupdate', async () => {
const currentTime = video.currentTime;
const duration = video.duration;
// 方法2a检查是否接近结束最后0.1秒)
if (duration - currentTime <= 0.1) {
console.log('视频即将播放完成');
// 处理即将结束的逻辑
if (videoFile === this.defaultVideo) {
let lastCurrentTime = 0;
video.addEventListener('timeupdate', async () => {
const currentTime = video.currentTime;
const duration = video.duration;
// 检查音频是否正在播放从minimaxi_stream.js获取isPlaying状态
const isAudioPlaying = window.isPlaying || false; // 需要确保isPlaying是全局可访问的
// 如果音频没有播放,且当前不是默认视频,则切换到默认视频
if (!isAudioPlaying) {
const currentVideoFile = this.currentVideo; // 获取当前播放的视频文件名
if (currentVideoFile !== this.defaultVideo) {
console.log('音频已停止,当前视频不是默认视频,准备切换到默认视频');
// 停止当前视频的循环
if (this.recordedVideo) {
this.recordedVideo.loop = false;
}
// 切换到默认视频
try {
await this.switchVideoWithReplaceTrack(this.defaultVideo, 'auto-switch', 'audio-ended');
console.log('已自动切换到默认视频');
} catch (error) {
console.error('自动切换到默认视频失败:', error);
}
}
}
lastCurrentTime = currentTime;
});
}
}
// 方法2b检查是否已经到达结尾
if (currentTime >= duration) {
console.log('视频播放完成');
// 处理播放完成的逻辑
}
// 方法2c检查时间是否停止更新可能表示播放结束
if (Math.abs(currentTime - lastCurrentTime) < 0.01) {
// 时间没有更新,可能播放结束或暂停
if (currentTime >= duration) {
console.log('视频播放完成(通过时间停止检测)');
}
}
lastCurrentTime = currentTime;
});
}
// 使用有限缓存策略最多缓存3个视频流 // 使用有限缓存策略最多缓存3个视频流
if (this.videoStreams.size >= 3) { if (this.videoStreams.size >= 3) {
@ -422,7 +486,7 @@ class WebRTCChat {
// }); // });
// 特别确保添加了5.mp4从日志看这是常用视频 // 特别确保添加了5.mp4从日志看这是常用视频
videosToPreload.add('5.mp4'); videosToPreload.add('6.mp4');
// 开始预加载 // 开始预加载
for (const videoFile of videosToPreload) { for (const videoFile of videosToPreload) {

View File

@ -1,11 +1,11 @@
// 以流式或非流式方式请求 minimaxi 大模型接口,并打印/返回内容 // 以流式或非流式方式请求 minimaxi 大模型接口,并打印/返回内容
// import { text } from "express"; // import { text } from "express";
window.isPlaying = false;
// 在文件顶部添加音频播放相关的变量和函数 // 在文件顶部添加音频播放相关的变量和函数
let audioContext = null; let audioContext = null;
let audioQueue = []; // 音频队列 let audioQueue = []; // 音频队列
let isPlaying = false; // let isPlaying = false;
let isProcessingQueue = false; // 队列处理状态 let isProcessingQueue = false; // 队列处理状态
let nextStartTime = 0; // 添加这行来声明 nextStartTime 变量 let nextStartTime = 0; // 添加这行来声明 nextStartTime 变量
@ -60,17 +60,17 @@ async function processAudioQueue() {
isProcessingQueue = true; isProcessingQueue = true;
console.log('开始处理音频队列'); console.log('开始处理音频队列');
let isFirstChunk = true; let isFirstChunk = true;
while (audioQueue.length > 0 || isPlaying) { while (audioQueue.length > 0 || window.isPlaying) {
// 如果当前没有音频在播放,且队列中有音频 // 如果当前没有音频在播放,且队列中有音频
if (!isPlaying && audioQueue.length > 0) { if (!window.isPlaying && audioQueue.length > 0) {
const audioItem = audioQueue.shift(); const audioItem = audioQueue.shift();
const sayName = 'say-5s-m-sw' const sayName = 'say-3s-m-sw'
const targetVideo = '5.mp4' const targetVideo = '6.mp4'
// 如果是第一个音频片段,触发视频切换 // 如果是第一个音频片段,触发视频切换
if (sayName != window.webrtcApp.currentVideoTag && window.webrtcApp && window.webrtcApp.handleTextInput) { if (sayName != window.webrtcApp.currentVideoTag && window.webrtcApp && window.webrtcApp.handleTextInput) {
try { try {
console.log('--------------触发视频切换:', sayName); console.log('--------------触发视频切换:', sayName);
await window.webrtcApp.switchVideoWithReplaceTrack(targetVideo, 'audio', 'say-5s-m-sw'); await window.webrtcApp.switchVideoWithReplaceTrack(targetVideo, 'audio', 'say-3s-m-sw');
isFirstChunk = false; isFirstChunk = false;
window.webrtcApp.currentVideoTag = sayName; window.webrtcApp.currentVideoTag = sayName;
} catch (error) { } catch (error) {
@ -104,19 +104,19 @@ function playAudioData(audioData) {
source.buffer = audioData; source.buffer = audioData;
source.connect(ctx.destination); source.connect(ctx.destination);
isPlaying = true; window.isPlaying = true;
source.onended = () => { source.onended = () => {
console.log('音频片段播放完成'); console.log('音频片段播放完成');
isPlaying = false; window.isPlaying = false;
resolve(); resolve();
}; };
// 超时保护 // 超时保护
setTimeout(() => { setTimeout(() => {
if (isPlaying) { if (window.isPlaying) {
console.log('音频播放超时,强制结束'); console.log('音频播放超时,强制结束');
isPlaying = false; window.isPlaying = false;
resolve(); resolve();
} }
}, (audioData.duration + 0.5) * 1000); }, (audioData.duration + 0.5) * 1000);
@ -126,7 +126,7 @@ function playAudioData(audioData) {
} catch (error) { } catch (error) {
console.error('播放音频失败:', error); console.error('播放音频失败:', error);
isPlaying = false; window.isPlaying = false;
resolve(); resolve();
} }
}); });