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

  • Xamarin Blueprints
  • Michael Williams
  • 232字
  • 2021-07-08 11:48:24

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.

主站蜘蛛池模板: 恩施市| 尚志市| 浦北县| 固阳县| 阜新市| 鸡泽县| 启东市| 唐河县| 页游| 柘荣县| 定襄县| 阳朔县| 大庆市| 西城区| 华蓥市| 西丰县| 涡阳县| 满洲里市| 澎湖县| 肇庆市| 临清市| 株洲市| 定襄县| 南皮县| 诸城市| 翁牛特旗| 曲沃县| 本溪| 依安县| 阿坝县| 商河县| 牟定县| 长岭县| 南丰县| 正蓝旗| 鄯善县| 张掖市| 滨海县| 三穗县| 黄梅县| 乌鲁木齐县|