1

I am following this documentation to convert text to speech using the Text To Speech REST API.

I'm successfully able to get a valid response using Postman and I'm able to pay the audio in PostMan. But I am not able to play the audio using JavaScript. Below is my Javascript code. I'm not sure what to do with the 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);
}

Thanks.

Ali Heikal
  • 3,790
  • 3
  • 18
  • 24
Anand Murali
  • 4,091
  • 1
  • 33
  • 46

1 Answers1

0

I referred to the following repository for my code in Java. It plays the audio in IDE and saves the audio file to your system.

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

  • Please include the solution in your actual answer, as it otherwise becomes useless once the linked resource becomes unavailable. – Capricorn Aug 29 '18 at 12:50