- 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.
推薦閱讀
- Node.js+Webpack開發實戰
- 深入理解Android(卷I)
- Python爬蟲開發:從入門到實戰(微課版)
- Python Network Programming Cookbook(Second Edition)
- MATLAB實用教程
- HTML5秘籍(第2版)
- MySQL程序員面試筆試寶典
- Java Web開發實例大全(基礎卷) (軟件工程師開發大系)
- Java EE 7 with GlassFish 4 Application Server
- 微信小程序開發邊做邊學(微課視頻版)
- AngularJS UI Development
- Java 7 Concurrency Cookbook
- Learning iOS Penetration Testing
- 劍指大數據:企業級電商數據倉庫項目實戰(精華版)
- Python程序員面試算法寶典