Microsoft Azure 认知服务 - 必应文本转语音 API - 使用 JavaScript 播放音频



我正在按照本文档使用文本到语音转换 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);
}

谢谢。

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

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

最新更新