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

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.

主站蜘蛛池模板: 张掖市| 麻栗坡县| 洱源县| 扎赉特旗| 竹溪县| 滁州市| 分宜县| 新龙县| 维西| 兴仁县| 肇庆市| 新巴尔虎左旗| 尚义县| 蒙自县| 策勒县| 封开县| 米林县| 上饶县| 清徐县| 金门县| 康保县| 平湖市| 佛学| 凯里市| 吉水县| 澄城县| 敦煌市| 旺苍县| 秀山| 新乐市| 保定市| 西乌珠穆沁旗| 门源| 龙门县| 泸水县| 民勤县| 建宁县| 临城县| 多伦县| 陕西省| 临漳县|