Archimede
Archimede

Reputation: 727

Windows Phone 8.1 vocal synthesis

I have a problem.

In my Windows Phone 8.1 app, I would implement a button function such that my phone tell "hello world" (by voice). I have already searched in the web dozens of solution, with the result: the instruction wasn't good for Win 8.1; the instruction has require external resources; the instruction gives me a lot of error.

Please, do you have a simple code to do this? Thank in advance!

Upvotes: 0

Views: 139

Answers (2)

steveybrown
steveybrown

Reputation: 303

This will work for Windows Phone 8.1, more info on Speech Synthesizer

    private async void TextToSpeech(string textToReadAloud)
    {
        SpeechSynthesizer ttssynthesizer = new SpeechSynthesizer();

        //Set the Voice & Speaker
        using (var speaker = new SpeechSynthesizer())
        {
            speaker.Voice = (SpeechSynthesizer.AllVoices.First(x => x.Gender == VoiceGender.Female));
            ttssynthesizer.Voice = speaker.Voice;
        }

        SpeechSynthesisStream ttsStream = await ttssynthesizer.SynthesizeTextToStreamAsync(textToReadAloud);
        MediaElement.SetSource(ttsStream, "");  
    }

Note MediaElement can be bound to a content control in your xaml.

            <ContentControl HorizontalAlignment="Left"         
            Width="320" Height="140" Content="{Binding MediaElement}"/>

Media Element declared in your view model.

    private MediaElement _mediaElement = new MediaElement();

    public MediaElement MediaElement
    {
        get
        {
            return _mediaElement;
        }
        set
        {
            Set(() => MediaElement, ref _mediaElement, value);
        }
    }

Upvotes: 1

Jogy
Jogy

Reputation: 2475

Have you tried using Windows.Media.SpeechSynthesis.SpeechSynthesizer?

For more information I suggest to watch the second lesson in MVA's course Universal Windows App Development with Cortana and the Speech SDK

Upvotes: 0

Related Questions