|
|
@@ -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
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|