Beispiel #1
0
        private async void Poem_Click(object sender, RoutedEventArgs e)
        {
            try // Basic error handling
            {
                // Disable the ListBoxes to prevent exceptions
                VoicesListBox.IsEnabled    = false;
                LanguagesListBox.IsEnabled = false;

                // Stop playing any Synthesized speech
                _speechSynthesizer.CancelAll();

                // Create SSML string
                var SsmlString = CreateSsmlWithXDocument2(GeneratePoem(2, 2));

                // Call SpeakTextAsync and await
                await _speechSynthesizer.SpeakSsmlAsync(SsmlString);

                MessageBox.Show("SSML Text read: '" + SsmlString + "'");

                // Re-enable the ListBoxes
                VoicesListBox.IsEnabled    = true;
                LanguagesListBox.IsEnabled = true;
            }
            catch (Exception ex) // Catch exceptions and display in MessageBox
            {
                MessageBox.Show(ex.Message,
                                "Exception",
                                MessageBoxButton.OK);
            }
        }
Beispiel #2
0
        private async void SaySsml_Click(object sender, RoutedEventArgs e)
        {
            try // error handling
            {
                // Disable the ListBoxes to prevent exceptions
                VoicesListBox.IsEnabled    = false;
                LanguagesListBox.IsEnabled = false;

                // Stop playing any Synthesized speech
                _speechSynthesizer.CancelAll();

                // Check that string is not null or empty
                if (!string.IsNullOrEmpty(PhraseTextBox.Text))
                {
                    // Create SSML string
                    var SsmlString = "<speak version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xml:lang=\"" + _speechSynthesizer.GetVoice().Language + "\">";
                    SsmlString += "<s>" + PhraseTextBox.Text + "</s></speak>";

                    // Call SpeakTextAsync and await
                    await _speechSynthesizer.SpeakSsmlAsync(SsmlString);

                    MessageBox.Show("SSML Text read: '" + SsmlString + "'");
                }
                else // if string is null or empty, display error message
                {
                    MessageBox.Show("Phrase text is required.",
                                    "Error",
                                    MessageBoxButton.OK);
                }

                // Re-enable the ListBoxes
                VoicesListBox.IsEnabled    = true;
                LanguagesListBox.IsEnabled = true;
            }
            catch (Exception ex) // Catch exception and call function to handle it.
            {
                HandleSpeechSynthesisError(ex);
            }
        }