JM_KA/assets/Script/Text_to_Speech.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-01-22 11:34:53 +08:00
const responsiveVoice = window["responsiveVoice"];
2022-01-21 11:01:58 +08:00
export default class Text_to_Speech {
//#region 外調參數
2022-01-20 19:20:17 +08:00
2022-01-22 11:34:53 +08:00
private voice: string = null;
2022-01-20 19:20:17 +08:00
2022-01-21 11:01:58 +08:00
//#endregion
2022-01-20 19:20:17 +08:00
2022-01-21 11:01:58 +08:00
//#region Custom
2022-01-20 19:20:17 +08:00
2022-01-21 11:01:58 +08:00
constructor() {
2022-01-22 11:34:53 +08:00
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);
2022-01-21 11:01:58 +08:00
}
2022-01-20 19:20:17 +08:00
2022-01-21 11:01:58 +08:00
public speak(msg: string): void {
let self: this = this;
2022-01-22 11:34:53 +08:00
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`);
}
2022-01-20 19:20:17 +08:00
}
2022-01-21 11:01:58 +08:00
//#endregion
2022-01-20 19:20:17 +08:00
}