Anand Murali
Anand Murali

Reputation: 4109

Microsoft Azure Cognitive Services - Bing Text to Speech API - Play audio using javascript

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.

Upvotes: 1

Views: 1141

Answers (1)

shreya kumar
shreya kumar

Reputation: 1

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

Upvotes: 0

Related Questions