1

我正在按照本文档使用 Text To Speech REST API 将文本转换为语音。

我成功地获得了有效的回复,Postman并且我能够以PostMan. 但我无法使用JavaScript. 下面是我的Javascript代码。我不确定如何处理response.

function bingSpeech(message) {
    var authToken = "TokenToCommunicateWithRestAPI";

    var http = new XMLHttpRequest();

    var params = `<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Female' name='Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)'>${message}</voice></speak>`;

    http.open('POST', 'https://speech.platform.bing.com/synthesize', true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-Type", "application/ssml+xml");
    http.setRequestHeader("Authorization", "bearer " + authToken);
    http.setRequestHeader("X-Microsoft-OutputFormat", "audio-16khz-32kbitrate-mono-mp3");

    http.onreadystatechange = function () {
        if (http.readyState == 4 && http.status == 200) {
            // I am getting the respone, but I'm not sure how to play the audio file. Need help here
        }
    }
    http.send(params);
}

谢谢。

4

1 回答 1

0

我在 Java 中引用了以下存储库以获取我的代码。它在 IDE 中播放音频并将音频文件保存到您的系统中。

https://github.com/Azure-Samples/Cognitive-Speech-TTS/tree/master/Samples-Http/Java/TTSSample/src/com/microsoft/cognitiveservices/ttssample

于 2018-08-29T12:34:51.727 回答