代码之家  ›  专栏  ›  技术社区  ›  Anand Murali

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

  •  1
  • Anand Murali  · 技术社区  · 6 年前

    我在跟踪这个 documentation 使用文本到语音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 回复  |  直到 6 年前
        1
  •  0
  •   shreya kumar    6 年前

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

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

    推荐文章