Search code examples
c#windows-phone-8.1voice

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!


Solution

  • 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);
            }
        }