Dennisaa

MainPageXamlcs-speech01

Feb 9th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Windows.Media.SpeechSynthesis;
  4. using Windows.UI.Xaml.Controls;
  5. using Windows.UI.Xaml.Media;
  6. using Windows.ApplicationModel.Resources.Core;
  7.  
  8. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
  9.  
  10. namespace App1 {
  11.     /// <summary>
  12.     /// An empty page that can be used on its own or navigated to within a Frame.
  13.     /// </summary>
  14.     public sealed partial class MainPage : Page {
  15.  
  16.         private SpeechSynthesizer synthesizer;
  17.         private ResourceContext speechContext;
  18.         private ResourceMap speechResourceMap;
  19.         private MediaElement _media = new MediaElement();
  20.  
  21.         public static MainPage Current;
  22.         public MainPage() {
  23.             this.InitializeComponent();
  24.             synthesizer = new SpeechSynthesizer();
  25.             speechContext = ResourceContext.GetForCurrentView();
  26.             speechContext.Languages = new string[] { SpeechSynthesizer.DefaultVoice.Language };
  27.             speechResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("LocalizationTTSResources");
  28.         }
  29.  
  30.         public List<Scenario> Scenarios
  31.         {
  32.             get { return this.scenarios; }
  33.         }
  34.  
  35.         private async void orangeButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
  36.  
  37.             if (_media.CurrentState.Equals(MediaElementState.Playing)) {
  38.                 _media.Stop();
  39.             }
  40.             else {
  41.                 string text = textBox1.Text.ToString();
  42.                 if (!String.IsNullOrEmpty(text)) {
  43.                     // Change the button label. You could also just disable the button if you don't want any user control.
  44.  
  45.  
  46.                     try {
  47.                         // Create a stream from the text. This will be played using a media element.
  48.                         SpeechSynthesisStream synthesisStream = await synthesizer.SynthesizeTextToStreamAsync(text);
  49.  
  50.                         // Set the source and start playing the synthesized audio stream.
  51.                         _media.AutoPlay = true;
  52.                         _media.SetSource(synthesisStream, synthesisStream.ContentType);
  53.                         _media.Play();
  54.                     }
  55.                     catch (System.IO.FileNotFoundException) {
  56.                         // If media player components are unavailable, (eg, using a N SKU of windows), we won't
  57.                         // be able to start media playback. Handle this gracefully
  58.  
  59.                         var messageDialog = new Windows.UI.Popups.MessageDialog("Media player components unavailable");
  60.                         await messageDialog.ShowAsync();
  61.                     }
  62.                     catch (Exception) {
  63.                         // If the text is unable to be synthesized, throw an error message to the user.
  64.  
  65.                         media.AutoPlay = false;
  66.                         var messageDialog = new Windows.UI.Popups.MessageDialog("Unable to synthesize text");
  67.                         await messageDialog.ShowAsync();
  68.                     }
  69.                 }
  70.             }
  71.  
  72.  
  73.  
  74.  
  75.         }
  76.  
  77.         //https://msdn.microsoft.com/en-us/library/windows.media.speechsynthesis.speechsynthesizer.aspx
  78.         private async void orangeButton2_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
  79.  
  80.             MediaElement mediaElement = _media;
  81.  
  82.             // The object for controlling the speech synthesis engine (voice).
  83.             var synth = new SpeechSynthesizer();
  84.  
  85.             // Generate the audio stream from plain text.
  86.             SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello World");
  87.  
  88.             // Send the stream to the media object.
  89.             mediaElement.SetSource(stream, stream.ContentType);
  90.             mediaElement.Play();
  91.         }
  92.  
  93.         private async void orangeButton3_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
  94.  
  95.             // The string to speak with SSML customizations.
  96.             string Ssml =
  97.                 @"<speak version='1.0' " +
  98.                 "xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-GB'>" +
  99.                 "Hello <prosody contour='(0%,+80Hz) (10%,+80%) (40%,+80Hz)'>World</prosody> " +
  100.                 "<break time='500ms' />" +
  101.                 "Goodbye <prosody rate='slow' contour='(0%,+20Hz) (10%,+30%) (40%,+10Hz)'>World</prosody>" +
  102.                 "</speak>";
  103.  
  104.             // The media object for controlling and playing audio.
  105.             MediaElement mediaElement = _media;
  106.  
  107.             // The object for controlling the speech synthesis engine (voice).
  108.             var synth = new SpeechSynthesizer();
  109.  
  110.             // Generate the audio stream from plain text.
  111.             SpeechSynthesisStream stream = await synth.SynthesizeSsmlToStreamAsync(Ssml);
  112.  
  113.             // Send the stream to the media object.
  114.             mediaElement.SetSource(stream, stream.ContentType);
  115.             mediaElement.Play();
  116.         }
  117.     }
  118. }
Add Comment
Please, Sign In to add comment