Windows 10 Speech: very basic code

No error handling, very dirty, just wanted to get something that produces sound.


    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <MediaElement x:Name="media" AutoPlay="False"/>
            <TextBox x:Name="textBox1" Text="My Dear Text" Margin="5"/>
            <Button x:Name="blueButton" Margin="5" Background="LightBlue" Content="ButtonRight" RelativePanel.RightOf="textBox1"/>
            <Button x:Name="orangeButton" Click="orangeButton_Click" Margin="5" Background="Orange" Content="ButtonBelow"
                    RelativePanel.RightOf="textBox1" RelativePanel.Below="blueButton"/>


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
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;
        public static MainPage Current;
        public MainPage() {
            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)) {
            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);
                    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();

Ref SSML... this worked... and the difference between loud and soft is perceptible:
string Ssml =
     @"<speak version='1.0' " +
     "xmlns='' xml:lang='en-GB'>" +
     "<prosody volume='x-loud'> This is extra loud volume. </prosody>";

This worked:
string Ssml =
               @"<speak version='1.0' " +
               "xmlns='' 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>" +

ref ssml:

Googling the above, see a lot of complaints about this. When I have time I will try this out:

I installed fresh Windows 10 and Visual Studio Community 2015, and the designer failed to load (for MainPage.xaml etc). I had to:

  1. enable developer mode in system settings (update section) as suggested in info dialog
  2. (re)install Visual C++ redistributable for VS 2015

But I don’t know which one exactly resolved the problem… Now the designer loads as expected. (I tried only C# universal app yet)


… and generally tidy up the post.