Esempio n. 1
0
        // good for a single sentence
        public string CreateSSMLWithXDocument()
        {
            XNamespace xmlns        = XNamespace.Get("http://www.w3.org/2001/10/synthesis");
            XDocument  ssmlDocument =
                new XDocument(
                    new XElement(xmlns + "speak",
                                 new XAttribute("version", "1.0"),
                                 new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                                 new XAttribute(XNamespace.Xml + "lang", "en-US"),
                                 new XElement("voice",
                                              new XAttribute("name", _speechSynthesizer.GetVoice().DisplayName),
                                              new XElement("p",
                                                           new XElement("s", PhraseTextBox.Text)
                                                           )
                                              )
                                 )
                    );

            return(ssmlDocument.ToString(SaveOptions.DisableFormatting));
        }
Esempio n. 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);
            }
        }