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

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

Android text-to-speech implementation

Now let's implement the IoC container and text to speech for Android. Start by creating a folder for the both the Android Modules and Services, add in two files to it, TextToSpeechDroid.cs and DroidModule.cs.

Let's start with the text to speech service; for TextToSpeechDroid.cs. And add the following:

public class TextToSpeechDroid :  Java.Lang.Object, ITextToSpeech, Android.Speech.Tts.TextToSpeech.IOnInitListener  
    { 
        private Android.Speech.Tts.TextToSpeech _speaker; 
 
        private string _toSpeak; 
 
        public void Speak (string msg) 
        { 
            var ctx = Forms.Context; 
            _toSpeak = msg; 
 
            if (_speaker == null)  
            { 
                _speaker = new Android.Speech.Tts.TextToSpeech (ctx, this); 
            }  
            else  
            { 
                var p = new Dictionary<string,string> (); 
                speaker.Speak (_toSpeak, QueueMode.Flush, p); 
            } 
        } 
 
        #region TextToSpeech.IOnInitListener implementation 
 
        public void OnInit (OperationResult status) 
        { 
            if (status.Equals (OperationResult.Success))  
            { 
                var p = new Dictionary<string,string> (); 
                _speaker.Speak (_toSpeak, QueueMode.Flush, p); 
            } 
        } 
 
        #endregion 
    } 

This IOnInitListener interface requires the OnInit function to be implemented. The OnInit function is called to signal the completion of the TextToSpeech engine initialization. We then implement the interface's function Speak to speak the text passed in. At the start of the function, we check to see that a new TextToSpeech object has been initialized; if we have then speak the message.

主站蜘蛛池模板: 申扎县| 宣化县| 温泉县| 榆树市| 二手房| 纳雍县| 澄城县| 湖北省| 扬中市| 霸州市| 施秉县| 东丽区| 攀枝花市| 丹巴县| 武强县| 商丘市| 浙江省| 蒙山县| 临朐县| 吉首市| 曲靖市| 莱芜市| 星子县| 仙桃市| 墨脱县| 新乐市| 勃利县| 宁化县| 古田县| 克山县| 泰顺县| 泉州市| 交口县| 陵水| 万全县| 田东县| 内黄县| 江西省| 鹤岗市| 勐海县| 中江县|