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

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.

主站蜘蛛池模板: 如东县| 满城县| 泗水县| 贵南县| 咸丰县| 井研县| 克山县| 湟中县| 靖安县| 桃园县| 玉山县| 台安县| 湖南省| 昌平区| 始兴县| 将乐县| 萨迦县| 万州区| 葵青区| 长丰县| 岐山县| 喀喇沁旗| 滕州市| 灵山县| 台南市| 阿拉善左旗| 石渠县| 墨脱县| 天柱县| 汉沽区| 扎囊县| 宁明县| 黑龙江省| 那坡县| 栾川县| 奉化市| 双城市| 惠水县| 铜陵市| 长兴县| 宝坻区|