Google_Text_to_Speech/assets/Script/Helloworld.ts

62 lines
47 KiB
TypeScript
Raw Permalink Normal View History

2022-05-15 05:15:46 +00:00
const { ccclass, property } = cc._decorator;
@ccclass
export default class Helloworld extends cc.Component {
protected onLoad(): void {
let base64string: string = "UklGRjSGAABXQVZFZm10IBAAAAABAAEAwF0AAIC7AAACABAAZGF0YRCGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAQABAAAAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABA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GetSound("你好");
2022-05-15 05:15:46 +00:00
}
public Sound(src: string): HTMLAudioElement {
let snd: HTMLAudioElement = new Audio(src);
snd.play();
return snd;
}
2022-05-15 05:32:03 +00:00
public GetSound(text: string) {
let self: this = this;
let data = JSON.stringify({
"audioConfig": {
"audioEncoding": "LINEAR16",
"pitch": 0,
"speakingRate": 1
},
"input": {
"text": text
},
"voice": {
"languageCode": "cmn-CN",
"name": "cmn-CN-Wavenet-C"
}
});
let xhr = new XMLHttpRequest();
xhr.withCredentials = true;
// xhr.addEventListener("readystatechange", function () {
// if (this.readyState === 4) {
// if (xhr.status >= 200 && xhr.status < 400) {
// let data: any = JSON.parse(this.responseText);
// self.Sound("data:audio/wav;base64," + data["audioContent"]);
// }
// }
// });
xhr.onreadystatechange = function (): void {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 400) {
let data: any = JSON.parse(this.responseText);
self.Sound("data:audio/wav;base64," + data["audioContent"]);
}
}
};
let apiKey = "AIzaSyCruaXR7Usg19RkAw5dc3iEfPSNdXJkhMA";
xhr.open("POST", `https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=${apiKey}`);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(data);
}
2022-05-15 05:15:46 +00:00
}