diff --git a/src/services/agora.js b/src/services/agora.js index 0005ce2..f492927 100644 --- a/src/services/agora.js +++ b/src/services/agora.js @@ -8,19 +8,10 @@ class AgoraService { this.isJoined = false; this.remoteUsers = {}; this.volumeIndicator = null; - this.vadEnabled = true; // 加入同一RTC频道 this.appid = '01a1debc964a4c6a8df1de2a6ce7aa4d'; this.token = '007eJxTYChiW7ib6cSzPW7fP2xqFVvPqf854sNXniin39cd3pu931SlwGBgmGiYkpqUbGlmkmiSbJZokZIG5BslmiWnmicmmqRcenA7vSGQkaFhbQArIwMEgvgiDMn5eWWJmUCyOD8nNd7QwtDAxJyBAQDAkSkW'; this.channel = 'convaiconsole_181047'; - - // VAD参数调整,检测语音段落 - this.vadParams = { - interruptDurationMs: 800, // 语音中断500毫秒视为一段话结束 - prefixPaddingMs: 300, // 在语音开始前预留的静音时间 - silenceDurationMs: 1200, // 静音持续1.2秒视为用户停止说话 - threshold: 0.5 // 判定为语音的阈值 - }; this.isListening = false; // 是否正在监听 this.isSpeaking = false; // 用户是否正在说话 @@ -33,9 +24,10 @@ class AgoraService { /** * Initialize the Agora RTC client */ - init() { + async init() { this.client = AgoraRTC.createClient({ mode: 'rtc', codec: 'vp8' }); this.setupEventListeners(); + return this.client; } @@ -124,16 +116,6 @@ class AgoraService { return; }; - // Enable VAD (Voice Activity Detection) - if (this.vadEnabled && this.localAudioTrack.setVADMode) { - this.localAudioTrack.setVADMode(true, this.vadParams); - - // 设置VAD回调,用于检测语音段落 - if (this.inConversation) { - this.setupVADCallback(); - } - } - // Publish local audio track console.log("this.localAudioTrack:",this.localAudioTrack); @@ -179,28 +161,6 @@ class AgoraService { isAudioMuted() { return this.localAudioTrack ? !this.localAudioTrack.enabled : true; } - - /** - * Set the VAD parameters - * @param {Object} params - The VAD parameters - */ - setVADParams(params) { - this.vadParams = { ...this.vadParams, ...params }; - if (this.localAudioTrack && this.localAudioTrack.setVADMode) { - this.localAudioTrack.setVADMode(this.vadEnabled, this.vadParams); - } - } - - /** - * Enable or disable VAD - * @param {boolean} enabled - Whether to enable VAD - */ - enableVAD(enabled) { - this.vadEnabled = enabled; - if (this.localAudioTrack && this.localAudioTrack.setVADMode) { - this.localAudioTrack.setVADMode(enabled, this.vadParams); - } - } /** * 开始对话状态 @@ -209,18 +169,6 @@ class AgoraService { startConversation(callback) { this.inConversation = true; this.speechSegmentCallback = callback; - - // 如果已经有音频轨道,设置VAD回调 - if (this.localAudioTrack && this.vadEnabled) { - this.setupVADCallback(); - } else { - // 如果没有音频轨道,先创建并发布 - this.startAudioPublishing().then(success => { - if (success && this.vadEnabled) { - this.setupVADCallback(); - } - }); - } } /** @@ -240,65 +188,6 @@ class AgoraService { this.recordedChunks = []; } - /** - * 设置VAD回调 - */ - setupVADCallback() { - // if (!this.localAudioTrack) return; - - if (!this.localAudioTrack) { - return; - } - if (typeof this.localAudioTrack.setVADMode !== 'function') { - console.error("当前 Agora SDK 版本不支持 VAD 功能");} - - // 设置VAD回调 - this.localAudioTrack.on('vad', (result) => { - - console.log("VAD 事件触发,result.state:", result.state, "result:", result); - - if (!this.inConversation) return; - - if (result.state === 'speech_start') { - // 语音开始 - this.isSpeaking = true; - this.segmentBuffer = ''; - - // 触发事件 - const event = new CustomEvent('speech-start'); - window.dispatchEvent(event); - } - else if (result.state === 'speech_end') { - // 语音结束 - this.isSpeaking = false; - - // 处理语音段落 - if (this.segmentBuffer && this.segmentBuffer.trim()) { - this.processSpeechSegment(this.segmentBuffer); - } - - // 清除缓冲区 - this.segmentBuffer = null; - - // 触发事件 - const event = new CustomEvent('speech-end'); - window.dispatchEvent(event); - } - else if (result.state === 'speech') { - // 正在说话,更新转写结果 - if (result.text) { - this.segmentBuffer = result.text; - - // 触发事件 - const event = new CustomEvent('speech-update', { - detail: { text: result.text } - }); - window.dispatchEvent(event); - } - } - }); - } - /** * 处理语音段落 * @param {string} text - 语音段落文本