Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Windows.Media.SpeechSynthesis;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Media;
- using Windows.ApplicationModel.Resources.Core;
- // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
- namespace App1 {
- /// <summary>
- /// An empty page that can be used on its own or navigated to within a Frame.
- /// </summary>
- public sealed partial class MainPage : Page {
- private SpeechSynthesizer synthesizer;
- private ResourceContext speechContext;
- private ResourceMap speechResourceMap;
- private MediaElement _media = new MediaElement();
- public static MainPage Current;
- public MainPage() {
- this.InitializeComponent();
- synthesizer = new SpeechSynthesizer();
- speechContext = ResourceContext.GetForCurrentView();
- speechContext.Languages = new string[] { SpeechSynthesizer.DefaultVoice.Language };
- speechResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("LocalizationTTSResources");
- }
- public List<Scenario> Scenarios
- {
- get { return this.scenarios; }
- }
- private async void orangeButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
- if (_media.CurrentState.Equals(MediaElementState.Playing)) {
- _media.Stop();
- }
- else {
- string text = textBox1.Text.ToString();
- if (!String.IsNullOrEmpty(text)) {
- // Change the button label. You could also just disable the button if you don't want any user control.
- try {
- // Create a stream from the text. This will be played using a media element.
- SpeechSynthesisStream synthesisStream = await synthesizer.SynthesizeTextToStreamAsync(text);
- // Set the source and start playing the synthesized audio stream.
- _media.AutoPlay = true;
- _media.SetSource(synthesisStream, synthesisStream.ContentType);
- _media.Play();
- }
- catch (System.IO.FileNotFoundException) {
- // If media player components are unavailable, (eg, using a N SKU of windows), we won't
- // be able to start media playback. Handle this gracefully
- var messageDialog = new Windows.UI.Popups.MessageDialog("Media player components unavailable");
- await messageDialog.ShowAsync();
- }
- catch (Exception) {
- // If the text is unable to be synthesized, throw an error message to the user.
- media.AutoPlay = false;
- var messageDialog = new Windows.UI.Popups.MessageDialog("Unable to synthesize text");
- await messageDialog.ShowAsync();
- }
- }
- }
- }
- //https://msdn.microsoft.com/en-us/library/windows.media.speechsynthesis.speechsynthesizer.aspx
- private async void orangeButton2_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
- MediaElement mediaElement = _media;
- // The object for controlling the speech synthesis engine (voice).
- var synth = new SpeechSynthesizer();
- // Generate the audio stream from plain text.
- SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello World");
- // Send the stream to the media object.
- mediaElement.SetSource(stream, stream.ContentType);
- mediaElement.Play();
- }
- private async void orangeButton3_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
- // The string to speak with SSML customizations.
- string Ssml =
- @"<speak version='1.0' " +
- "xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-GB'>" +
- "Hello <prosody contour='(0%,+80Hz) (10%,+80%) (40%,+80Hz)'>World</prosody> " +
- "<break time='500ms' />" +
- "Goodbye <prosody rate='slow' contour='(0%,+20Hz) (10%,+30%) (40%,+10Hz)'>World</prosody>" +
- "</speak>";
- // The media object for controlling and playing audio.
- MediaElement mediaElement = _media;
- // The object for controlling the speech synthesis engine (voice).
- var synth = new SpeechSynthesizer();
- // Generate the audio stream from plain text.
- SpeechSynthesisStream stream = await synth.SynthesizeSsmlToStreamAsync(Ssml);
- // Send the stream to the media object.
- mediaElement.SetSource(stream, stream.ContentType);
- mediaElement.Play();
- }
- }
- }
Add Comment
Please, Sign In to add comment