Microsoft Azure认知服务-Bing文本到语音API-使用javascript播放音频

时间:2018-08-09 07:54:50

标签: text-to-speech microsoft-cognitive azure-cognitive-services bing-speech

我正在遵循此documentation,以使用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);
}

谢谢。

1 个答案:

答案 0 :(得分:0)

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

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