瀏覽代碼

多并发

dangdang 1 月之前
父節點
當前提交
fb1038f024
共有 1 個文件被更改,包括 43 次插入34 次删除
  1. 43 34
      src/views/ChatTts.vue

+ 43 - 34
src/views/ChatTts.vue

@@ -247,40 +247,6 @@ const getNextAudioToPlay = () => {
 };
 
 // 播放音频的方法
-const audioContext = new (window.AudioContext || window.webkitAudioContext)();
-
-const playAudio = (audioData) => {
-  return new Promise((resolve, reject) => {
-    const blob = new Blob([audioData], { type: "audio/wav" });
-    const url = URL.createObjectURL(blob);
-
-    fetch(url)
-      .then((res) => res.arrayBuffer())
-      .then((arrayBuffer) => audioContext.decodeAudioData(arrayBuffer))
-      .then((audioBuffer) => {
-        const source = audioContext.createBufferSource();
-        source.buffer = audioBuffer;
-        source.connect(audioContext.destination);
-        source.start(0);
-
-        source.onended = () => {
-          URL.revokeObjectURL(url);
-          resolve();
-        };
-
-        source.onerror = (error) => {
-          console.error("音频播放错误:", error);
-          URL.revokeObjectURL(url);
-          reject(error);
-        };
-      })
-      .catch((error) => {
-        console.error("音频加载或解码时出错:", error);
-        reject(error);
-      });
-  });
-};
-
 // const playAudio = (audioData) => {
 //   return new Promise((resolve, reject) => {
 //     const audioContext = new (window.AudioContext || window.webkitAudioContext)();
@@ -323,6 +289,49 @@ const playAudio = (audioData) => {
 //   });
 // };
 
+// 播放音频的方法
+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 playTTS1 = async (ttsMessage) => {
 //   try {
 //     const res = await axios.post(