官术网_书友最值得收藏!

WinPhone text-to-speech implementation

Now we go back to Windows Phone for the last implementation. See how tricky it can be when you have to switch between multiple platforms. Imagine if we had to change languages and re-write IoC containers; the amount of work would be much greater. Not only that, there would be no point in using IoC, because we cannot share any code.

So firstly, don't forget to import the nuget package for Autofac:

Now that we have access to the Autofac framework, let's continue implementing the text to speech service. Start with adding a new folder called Services, then add the TextToSpeechWinPhone.cs file and implement it:

public class TextToSpeechWinPhone : ITextToSpeech 
        { 
            public async void Speak(string text) 
            { 
                MediaElement mediaElement = new MediaElement (); 
 
                var synth = new Windows.Media.SpeechSynthesis. SpeechSynthesizer (); 
 
                SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(text); 
 
                mediaElement.SetSource(stream, stream.ContentType); 
                mediaElement.Play(); 
            } 
        } 

Looking at this more closely, you can see the instantiation of MediaElement; this is used to play an audio source. Our source in this case is SpeechSynthesisStream; this stream is built via a speech synthesizer. When we call the function SynthesizeTextToStreamAsync, it will be an audio stream based on the text inserted into this function. We then set the MediaElement source to the stream and call the Play function to begin speaking. One addition to configuring Windows Phone is checking the capability in the app manifest file.

主站蜘蛛池模板: 修水县| 合山市| 印江| 丽江市| 盐山县| 句容市| 镇雄县| 城口县| 乃东县| 介休市| 平度市| 那坡县| 工布江达县| 宝应县| 民权县| 寿光市| 郯城县| 揭东县| 紫金县| 北票市| 准格尔旗| 商洛市| 壤塘县| 衡山县| 青岛市| 珲春市| 安庆市| 卢氏县| 石棉县| 桓台县| 改则县| 封开县| 屏东市| 沙湾县| 张掖市| 宜兰县| 锦州市| 泰顺县| 商南县| 太湖县| 祁连县|