From 587caaa9b42a487ca9faf3386944b0fa3b9c1a8c Mon Sep 17 00:00:00 2001 From: JianMiau Date: Sat, 22 Jan 2022 11:34:53 +0800 Subject: [PATCH] [add] Text_to_Speech --- assets/Plugins/responsivevoice.js.meta | 2 +- assets/Script/Manager.ts | 5 ++-- assets/Script/Text_to_Speech.ts | 34 ++++++++++++++++++-------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/assets/Plugins/responsivevoice.js.meta b/assets/Plugins/responsivevoice.js.meta index 0a74a6c..5644421 100644 --- a/assets/Plugins/responsivevoice.js.meta +++ b/assets/Plugins/responsivevoice.js.meta @@ -4,6 +4,6 @@ "isPlugin": true, "loadPluginInWeb": true, "loadPluginInNative": true, - "loadPluginInEditor": true, + "loadPluginInEditor": false, "subMetas": {} } \ No newline at end of file diff --git a/assets/Script/Manager.ts b/assets/Script/Manager.ts index 2b640df..5aabd33 100644 --- a/assets/Script/Manager.ts +++ b/assets/Script/Manager.ts @@ -16,7 +16,7 @@ export default class Manager extends cc.Component { //#region private - private _text_to_Speech: Text_to_Speech = new Text_to_Speech(); + private _text_to_Speech: Text_to_Speech; //#endregion @@ -24,6 +24,7 @@ export default class Manager extends cc.Component { protected onLoad(): void { let self: this = this; + this._text_to_Speech = new Text_to_Speech(); window.addEventListener("message", function (e: MessageEvent): void { let data: any = e.data; let method: string = data.method; @@ -73,7 +74,7 @@ export default class Manager extends cc.Component { console.log(msg); } - private _speak(msg: string): void { + public Speak(msg: string): void { this._text_to_Speech.speak(msg); } diff --git a/assets/Script/Text_to_Speech.ts b/assets/Script/Text_to_Speech.ts index 3be2a97..836d6a0 100644 --- a/assets/Script/Text_to_Speech.ts +++ b/assets/Script/Text_to_Speech.ts @@ -1,26 +1,40 @@ +const responsiveVoice = window["responsiveVoice"]; + export default class Text_to_Speech { //#region 外調參數 - private _responsiveVoice: any = window["responsiveVoice"]; + private voice: string = null; //#endregion //#region Custom constructor() { - this._responsiveVoice.speak("hello world"); + let self: this = this; + responsiveVoice.init(); + var voicelist = responsiveVoice.getVoices(); + this.setDefaultVoice(voicelist[14].name); + } + + public setDefaultVoice(voice: string): void { + this.voice = voice; + responsiveVoice.setDefaultVoice(voice); } public speak(msg: string): void { let self: this = this; - this._responsiveVoice.speak(msg, "Chinese Female", { - onstart: function (Callback: any): void { - console.log(`onstart msg: ${msg}`); - }, - onend: function (Callback: any): void { - console.log(`onstart msg: ${msg}`); - }, - }); + if (responsiveVoice.voiceSupport()) { + responsiveVoice.speak(msg, this.voice, { + onstart: function (Callback: any): void { + console.log(`onstart msg: ${msg}`); + }, + onend: function (Callback: any): void { + console.log(`onstart msg: ${msg}`); + }, + }); + } else { + console.error(`!responsiveVoice.voiceSupport`); + } } //#endregion