dangdang 1 ヶ月 前
コミット
c9166a0751
1 ファイル変更6 行追加46 行削除
  1. 6 46
      src/views/ChatTts.vue

+ 6 - 46
src/views/ChatTts.vue

@@ -59,7 +59,7 @@ const updateChatList = async (message = "", loading = true) => {
 };
 
 // 创建一个队列实例,设置并发数为 1
-const queue = new PQueue({ concurrency: 4 });
+const queue = new PQueue({ concurrency: 3 });
 const queue1 = new PQueue({ concurrency: 1 });
 const queue2 = new PQueue({ concurrency: 1 });
 
@@ -158,7 +158,10 @@ const playTTS = async (ttsMessage) => {
         `/openapi-prd/ai/ttt/synthesize?text=${ttsMessage}&sessionId=${sessionId.value}`,
         { responseType: "arraybuffer" }
       );
-
+      if (!res.data || res.data.byteLength === 0) {
+        console.error("音频数据无效,无法播放.");
+        return;
+      }
       // 按顺序存入音频队列
       audioQueue.push({ orderId: currentOrderId, audioData: res.data });
 
@@ -183,6 +186,7 @@ const playNextAudio = async () => {
 
   // 找到队列中最小序号的音频,确保顺序播放
   const nextAudio = getNextAudioToPlay();
+  console.log(nextAudio,"没有找到下一个应该播放的音频,队列尚未按顺序准备好.");
 
   // 如果找不到下一个应该播放的音频,表示队列还没有按顺序准备好
   if (!nextAudio) {
@@ -246,49 +250,6 @@ const getNextAudioToPlay = () => {
   return null;  // 如果没有找到匹配的音频,返回 null
 };
 
-// 播放音频的方法
-// const playAudio = (audioData) => {
-//   return new Promise((resolve, reject) => {
-//     const audioContext = new (window.AudioContext || window.webkitAudioContext)();
-//     const blob = new Blob([audioData], { type: "audio/wav" });
-//     const url = URL.createObjectURL(blob);
-//     const audioBufferSourceNode = audioContext.createBufferSource();
-
-//     fetch(url)
-//         .then((response) => {
-//           if (!response.ok) {
-//             console.error("音频文件请求失败: ", response.statusText);
-//             reject(new Error("音频文件请求失败"));
-//             return;
-//           }
-//           return response.arrayBuffer();
-//         })
-//         .then((arrayBuffer) => audioContext.decodeAudioData(arrayBuffer))
-//         .then((audioBuffer) => {
-//           audioBufferSourceNode.buffer = audioBuffer;
-//           audioBufferSourceNode.connect(audioContext.destination);
-
-//           audioBufferSourceNode.start(0);
-
-//           // 当音频播放完毕时,调用 resolve
-//           audioBufferSourceNode.onended = () => {
-//             URL.revokeObjectURL(url);
-//             resolve();  // 播放完成后,继续播放下一个
-//           };
-
-//           audioBufferSourceNode.onerror = (error) => {
-//             console.error("音频播放错误:", error);
-//             URL.revokeObjectURL(url);
-//             reject(error);  // 播放失败时,返回错误
-//           };
-//         })
-//         .catch((error) => {
-//           console.error("音频加载或解码时出错:", error);
-//           reject(error);  // 如果解码或播放出错,reject
-//         });
-//   });
-// };
-
 // 播放音频的方法
 const playAudio = (audioData) => {
   return new Promise((resolve, reject) => {
@@ -331,7 +292,6 @@ const playAudio = (audioData) => {
         });
   });
 };
-
 // const playTTS1 = async (ttsMessage) => {
 //   try {
 //     const res = await axios.post(