public class AzureTextToSpeechService {
private SpeechSynthesizer? synthesizer;
public async Task ReadOutLoudAsync(string text) {
AzureKeyCredential credentials = new(key);
TextAnalyticsClient textClient = new(endpoint, credentials);
Response<DetectedLanguage> response = textClient.DetectLanguage(text);
DetectedLanguage lang = response.Value;
var config = SpeechConfig.FromSubscription(
ConstantsHelpers.AZURE_SPEECH_KEY,
ConstantsHelpers.AZURE_SPEECH_REGION);
switch (lang.Iso6391Name) {
case "en":
config.SpeechSynthesisVoiceName = "Microsoft Server Speech Text to Speech Voice (en-US, SaraNeural)";
config.SpeechSynthesisLanguage = "en-us";
break;
case "es":
config.SpeechSynthesisVoiceName = "Microsoft Server Speech Text to Speech Voice (es-VE, PaolaNeural)";
config.SpeechSynthesisLanguage = "es-VE";
break;
}
synthesizer = new SpeechSynthesizer(config);
IsSpeaking = true;
await synthesizer.SpeakTextAsync(text);
}
public async Task StopSpeaking() {
if (synthesizer != null && IsSpeaking == true) {
await synthesizer.StopSpeakingAsync();
]
}
}
}
public class AzureTextToSpeechService {
private SpeechSynthesizer? synthesizer;
public async Task ReadOutLoudAsync(string text) {
AzureKeyCredential credentials = new(key);
TextAnalyticsClient textClient = new(endpoint, credentials);
Response<DetectedLanguage> response = textClient.DetectLanguage(text);
DetectedLanguage lang = response.Value;
var config = SpeechConfig.FromSubscription(
ConstantsHelpers.AZURE_SPEECH_KEY,
ConstantsHelpers.AZURE_SPEECH_REGION);
switch (lang.Iso6391Name) {
case "en":
config.SpeechSynthesisVoiceName = "Microsoft Server Speech Text to Speech Voice (en-US, SaraNeural)";
config.SpeechSynthesisLanguage = "en-us";
break;
case "es":
config.SpeechSynthesisVoiceName = "Microsoft Server Speech Text to Speech Voice (es-VE, PaolaNeural)";
config.SpeechSynthesisLanguage = "es-VE";
break;
}
synthesizer = new SpeechSynthesizer(config);
IsSpeaking = true;
await synthesizer.SpeakTextAsync(text);
}
public async Task StopSpeaking() {
if (synthesizer != null && IsSpeaking == true) {
await synthesizer.StopSpeakingAsync();
]
}
}
}