Browse Source

语音优化

林旭祥 10 months ago
parent
commit
a1ef36c68b
1 changed files with 7 additions and 2 deletions
  1. 7 2
      src/utils/speech.ts

+ 7 - 2
src/utils/speech.ts

@@ -1,6 +1,7 @@
 import Speech from 'speak-tts';
 import http from '@/api';
 let speech: any = null;
+let myAudio: any = null;
 let browserSupport: boolean = false;
 let speechText: string = '';
 
@@ -98,7 +99,8 @@ export const speckText = (text: any) => {
   if (obj[text]) {
     //用本地文件
     let url = `./static/audio/${obj[text]}`;
-    new Audio(url).play();
+    myAudio = new Audio(url);
+    myAudio.play();
     speechText = '';
   } else {
     if (browserSupport == true) {
@@ -111,7 +113,8 @@ export const speckText = (text: any) => {
       //用百度语音
       let tok = localStorage.getItem('tok') || '';
       let url = `https://tsn.baidu.com/text2audio?tex=${encodeURI(text)}&tok=${tok}&cuid=baike&lan=ZH&ctp=1&vol=15&rate=32&per=0&spd=7&pit=4`;
-      new Audio(url).play();
+      myAudio = new Audio(url);
+      myAudio.play();
       speechText = '';
     }
   }
@@ -121,6 +124,8 @@ export const speckText = (text: any) => {
 export const speckCancel = () => {
   if (speech && browserSupport == true) {
     speech.cancel();
+  } else {
+    myAudio.pause();
   }
 };