C#C
C#3y ago
eduardoA

❔ My Azure service do not Stop

I have a text to speech service, and for some reason, when I press the button to stop it dosent do anything

   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();
]
            }
        }
    }


I call like this

private void StopSpeakButtonAction() {
            AzureTextToSpeechService.StopSpeech();
            IsPlayingEnabled = true;
            IsStopEnabled = false;
        }

        private async void SpeakButtonction() {
            await AzureTextToSpeechService.ReadOutLoudAsync(Text);
            IsPlayingEnabled = false;
            IsStopEnabled = true;
        }
Was this page helpful?