The following code authenticates with the text-to-speech service from a react.js project running in the browser using a bearer token:
import TextToSpeechV1 from 'ibm-watson/text-to-speech/v1';
import {BearerTokenAuthenticator} from 'ibm-cloud-sdk-core'
submit = () => {
const token = '<the bearer token>'
const url = '<the service url>'
const textToSpeech = new TextToSpeechV1({
authenticator: new BearerTokenAuthenticator({bearerToken: token}),
serviceUrl: url,
});
const params = {
text: 'This is my text.',
voice: 'en-US_MichaelV3Voice',
accept: 'audio/mp3'
};
textToSpeech
.synthesize(params)
.then(response => {
console.log(response.result.length);
// make a download link
const myURL = window.URL || window.webkitURL
var binaryData = [];
binaryData.push(response.result);
const url = myURL.createObjectURL(new Blob(binaryData, {type: "audio/mp3"}))
document.querySelector("a").href = url;
})
.catch(err => {
console.log(err);
});
}
After clicking the download link, I get an MP3 file of reasonable size having `ID3`, `#TSSE` and `Lavf58.29.100` at the beginning, but it does not play in any of the players I tried. What am I missing?
------------------------------
Meir Goldenberg
------------------------------
#EmbeddableAI#WatsonAPIs