simon 4 месяцев назад
Родитель
Сommit
fbf9d4686f

+ 1 - 1
commans/utils/Index.ets

@@ -1 +1 @@
-export { MainPage } from './src/main/ets/components/MainPage';
+export { Speaker } from './src/main/ets/utils/Speaker';

+ 0 - 19
commans/utils/src/main/ets/components/MainPage.ets

@@ -1,19 +0,0 @@
-@Component
-export struct MainPage {
-  @State message: string = 'Hello World';
-
-  build() {
-    Row() {
-      Column() {
-        Text(this.message)
-          .fontSize($r('app.float.page_text_font_size'))
-          .fontWeight(FontWeight.Bold)
-          .onClick(() => {
-            this.message = 'Welcome';
-          })
-      }
-      .width('100%')
-    }
-    .height('100%')
-  }
-}

+ 58 - 0
commans/utils/src/main/ets/utils/Speaker.ets

@@ -0,0 +1,58 @@
+import { textToSpeech } from '@kit.CoreSpeechKit';
+import { BusinessError, emitter } from '@kit.BasicServicesKit';
+
+export class Speaker {
+  ttsEngin?: textToSpeech.TextToSpeechEngine;
+  initParamsInfo: textToSpeech.CreateEngineParams = {
+    language: 'zh-CN',
+    person: 0,
+    online: 1,
+    extraParams: { "style": 'interaction-broadcast', "locate": 'CN', "name": 'EngineName' }
+  };
+  speakListener?: textToSpeech.SpeakListener;
+
+  constructor() {
+    this.initListener();
+    this.createEngin();
+  }
+
+  initListener() {
+    this.speakListener = {
+      onStart(requestId: string, response: textToSpeech.StartResponse) {
+      },
+      onComplete(requestId: string, response: textToSpeech.CompleteResponse) {
+        if (response.type === 1) {
+          emitter.emit("eventId");
+        }
+      },
+      onStop(requestId: string, response: textToSpeech.StopResponse) {
+        if (response.type === 1) {
+          emitter.emit("eventId");
+        }
+      },
+      onData(requestId: string, audio: ArrayBuffer, response: textToSpeech.SynthesisResponse) {
+
+      },
+      onError(requestId: string, errorCode: number, errorMessage: string) {
+
+      }
+
+    };
+  }
+
+  createEngin() {
+    try {
+      textToSpeech.createEngine(this.initParamsInfo,
+        (err: BusinessError, textToSpeechEngine: textToSpeech.TextToSpeechEngine) => {
+          if (!err) {
+            this.ttsEngin = textToSpeechEngine;
+            this.ttsEngin.setListener(this.speakListener);
+          } else {
+          }
+        });
+    } catch (error) {
+      let message = (error as BusinessError).message;
+      let code = (error as BusinessError).code
+    }
+  }
+}