加载中

In Windows Phone there are great features to communicate with an app using voice. In previous post I have described how to command your app using your voice. In this post I will cover on how the app can respond using text to speech. The feature text to speech can be used in various scenarios to give the user feedback and is definitely a great tool to optimize your apps accessibility. The entire source code can be downloaded at the end of the post.

Windows Phone have support for 15 languages (sadly, not Swedish) and each language comes with two voices, one male and one female. The phone has one voice set as default. This can be viewed and changed in the phone settings. The default voice is the one to be used if not the app say otherwise.

windows phone settings

windows phone settings
speech settings

speech settings

在windows phone中有一个很大的特性就是通过语音与app交互。在上一章节中,我已经描述了如何通过语音命令你的app。在这一章节中我将讲解app如何通过文本转语音回应。文本转语音的回应可以被用在涉及反馈用户的各种场景里,而且这的确是个提高你app可访问性的好工具。整个源码可以在本章的结尾处下载。

windows phone支持15种语言(抱歉,没有瑞典语),每种语言附带两种声音,男声和女声。手机会默认一种声音。这个可以在手机的设置功能里面可以查看和修改。如果app没有被修改的话,默认的声音就是一般被使用的那个了。

windows phone settings

windows phone设置界面

speech settings

speech 设置界面

First of all we create a simple UI with three buttons supporting our three scenarios. Add event handlers to the buttons.

main ui

main ui

Now we need to add speech recognition capability to the app. If not, we will get a security error when we try to speak any text. Open the app manifest and add the capability as shown in the picture below.

Speech recognition capability

Speech recognition capability

Now we will dig into the code and start with scenario number 1. The crazy thing here is that only two lines of code are needed to speech text with the default voice. First of all we create a new method and in that we create a SpeechSynthesizer object that will be used to speak the text we will provide for it. Then we will use the method SpeakTextAsync on the synthesizer-object and send in the text. Note that this method is async so we need to decorate our method with the async keyword.

         private async void GreetInEnglish()
        {
            //Fist create the synth that will be playing the sound
            SpeechSynthesizer synth = new SpeechSynthesizer();
            //await the synth while it speaks the text of your choice. This will use the default voice.
            await synth.SpeakTextAsync("Hello man! How are you today? Well i'm just fine myself!");
        }

Run the app and hear it greet you! :)

首先,我们需要创建一个有三个按钮的简单用户图形界面来支持我们三个场景,并且给每个按钮添加事件处理。

main ui

主用户图形界面

现在我们需要对app添加语音识别功能,不然的话,当我们试着对app说任何内容的话将会得到一个安全错误提示。打开app显示列表然后添加下图所展示的功能。

Speech recognition capability

语音识别功能

现在我们要开始研究我们的代码并且开始第一部分场景了。这里疯狂的事情就是仅需两行代码就可以转换为默认语音的语音文件。首先,我们创建一个新方法,然后在里面创建一个SpeechSynthesizer对象,这个对象将被用于我们自己提供的说的内容。然后我们用SpeakTextAsync中的SpeakTextAsync方法,并且以text的形式发送。注意这个方法异步的,因此我们需要用异步关键字来修饰这个方法,

  private async void GreetInEnglish()
        {
            //Fist create the synth that will be playing the sound
            SpeechSynthesizer synth = new SpeechSynthesizer();
            //await the synth while it speaks the text of your choice. This will use the default voice.
            await synth.SpeakTextAsync("Hello man! How are you today? Well i'm just fine myself!");
        }
运行app,你将会听到他在同你打招呼哦! :)

Now for scenario no 2 we will start out as in scenario 1 with a new method. Now we will tell the phone to use a female French voice. This is easily done by querying the InstalledVoices.All for a voice that is of culture “fr-FR” and female. Then we will tell the SpeechSynthesizer-object to use that voice. I have used Google-translate to translate the text from the previous sample to French. This will make the speech sound more natural J

 
        private async void GreetInFrench()
        {
            //Create the synth that will be playing the sound
            SpeechSynthesizer synth = new SpeechSynthesizer();

            //Find the voice of your choice. In this case french woman
            var voice = (from x in InstalledVoices.All
                     where x.Language == "fr-FR" &&
                     x.Gender == VoiceGender.Female
                     select x).FirstOrDefault();

            //Tell the synth to use the selected voice
            synth.SetVoice(voice);

            //await the synth while it speaks the text of your choice with the selected voice
            await synth.SpeakTextAsync("Bonjour homme! Comment allez-vous aujourd'hui? Eh bien, je suis juste moi-même très bien!");
        }

For our final scenario we will create a method that will loop through all the installed voices and let them say “Hello World”. Just as in the previous example we will use the installed voices, but this time we will loop over them and let each and one of them speak the text.

 
        private async void LoopAllVoices()
        {
            //Create the synth that will be playing the sound
            SpeechSynthesizer synth = new SpeechSynthesizer();

            //Loop all the installed voices
            foreach (var voice in InstalledVoices.All)
            {
                //Tell the synth to use the selected voice
                synth.SetVoice(voice);
                //await the synth while it speaks the text with the current voice
                await synth.SpeakTextAsync("Hello world!");
            }
        }

Download the source code and have fun! :)

download source code

现在对于第二部分,我们将像第一部分一样创建一个新方法。我们将用法国的女性声音同手机说话。这个通过查询InstalledVoices很容易就做到了。所获取的就是一种“fr-FR”文化的女性声音。然后我们将告诉SpeechSynthesizer对象去用这个声音。我已经用谷歌翻译把先前的文本例子翻译成了法语。这样使说话听起来会自然些。

private async void GreetInFrench()
        {
            //Create the synth that will be playing the sound
            SpeechSynthesizer synth = new SpeechSynthesizer();

            //Find the voice of your choice. In this case french woman
            var voice = (from x in InstalledVoices.All
                     where x.Language == "fr-FR" &&
                     x.Gender == VoiceGender.Female
                     select x).FirstOrDefault();

            //Tell the synth to use the selected voice
            synth.SetVoice(voice);

            //await the synth while it speaks the text of your choice with the selected voice
            await synth.SpeakTextAsync("Bonjour homme! Comment allez-vous aujourd'hui? Eh bien, je suis juste moi-même très bien!");
        }
我们最终的部分是创建一个 可以 用所有 安装的声音让他们依次说“hello world 方法。正如在先前的例子一样,我们将用已经安装的声音,但是这一次我们将对他们进行循环然后让他们依次说出文本内容。
private async void LoopAllVoices()
        {
            //Create the synth that will be playing the sound
            SpeechSynthesizer synth = new SpeechSynthesizer();

            //Loop all the installed voices
            foreach (var voice in InstalledVoices.All)
            {
                //Tell the synth to use the selected voice
                synth.SetVoice(voice);
                //await the synth while it speaks the text with the current voice
                await synth.SpeakTextAsync("Hello world!");
            }
        }
下载源码,希望你玩的开心哦! :)
返回顶部
顶部