Task Speak(CancellationToken?cancelToken) { if (string.IsNullOrWhiteSpace(text)) { return(Task.CompletedTask); } if (language.HasValue && !string.IsNullOrWhiteSpace(language.Value.Language)) { Locale locale = null; if (!string.IsNullOrWhiteSpace(language.Value.Country)) { locale = new Locale(language.Value.Language, language.Value.Country); } else { locale = new Locale(language.Value.Language); } var result = textToSpeech.IsLanguageAvailable(locale); if (result == LanguageAvailableResult.CountryAvailable) { textToSpeech.SetLanguage(locale); } else { Console.WriteLine("Locale: " + locale + " was not valid, setting to default."); SetDefaultLanguage(); } } else { SetDefaultLanguage(); } var tcs = new TaskCompletionSource <object>(); cancelToken?.Register(() => { textToSpeech.Stop(); tcs.TrySetCanceled(); }); textToSpeech.SetPitch(pitch); textToSpeech.SetSpeechRate(speakRate); textToSpeech.SetOnUtteranceProgressListener(new TtsProgressListener(tcs)); #pragma warning disable CS0618 // Type or member is obsolete count++; var map = new Dictionary <string, string> { [Android.Speech.Tts.TextToSpeech.Engine.KeyParamUtteranceId] = count.ToString() }; textToSpeech.Speak(text, QueueMode.Flush, map); #pragma warning restore CS0618 // Type or member is obsolete return(tcs.Task); }
public AndroidTextToSpeech(AndroidSensusService service) { _textToSpeech = new TextToSpeech(service, this); _initWait = new ManualResetEvent(false); _utteranceWait = new ManualResetEvent(false); _disposed = false; _textToSpeech.SetOnUtteranceProgressListener(this); }
public Task Init() { if (initialized) { return(Task.FromResult(true)); } this.initTcs = new TaskCompletionSource <bool>(); Console.WriteLine("Current version: " + (int)global::Android.OS.Build.VERSION.SdkInt); Android.Util.Log.Info("CrossTTS", "Current version: " + (int)global::Android.OS.Build.VERSION.SdkInt); textToSpeech = new Android.Speech.Tts.TextToSpeech(Application.Context, this); listener = new TtsProgressListener(); textToSpeech.SetOnUtteranceProgressListener(listener); return(this.initTcs.Task); //bool hasThrown = false; //bool isValid = false; //while (!isValid) //{ // try // { // textToSpeech.Speak("Initializing", QueueMode.Add, null, null); // isValid = true; // } // catch (Exception e) // { // if (!hasThrown) // { // hasThrown = true; // Android.Util.Log.Debug("TTS", "(At least one) failure to initialize TTS."); // } // } //} //return Task.CompletedTask; }