To get a list of available voices for Azure Text-to-Speech using JavaScript, you can make a call to the Azure Text-to-Speech API’s endpoint for listing voices.
Here is an example:
JavaScript :
const subscriptionKey = 'YOUR_SUBSCRIPTION_KEY';
const region = 'YOUR_REGION';
async function getVoicesList() {
const response = await fetch(`https://${region}.tts.speech.microsoft.com/cognitiveservices/voices/list`, {
headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey
}
});
if (!response.ok) {
throw new Error('Error fetching voices list');
}
const voices = await response.json();
return voices;
}
getVoicesList()
.then(voices => console.log(voices))
.catch(error => console.error('Error:', error));
- Subscription Key and Region: Replace
YOUR_SUBSCRIPTION_KEY
andYOUR_REGION
with your Azure subscription key and region. - Fetch Request: The
fetch
API is used to make an HTTP GET request to the Azure Text-to-Speech voices list endpoint. - Headers: The
Ocp-Apim-Subscription-Key
header is set to authenticate the request. - Response Handling: The response is checked for success, then parsed as JSON to get the list of voices.
This will give you the list of available voices that you can use with Azure Text-to-Speech.
Leave a Reply