protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById <Button>(Resource.Id.myButton);

            button.Click += (object sender, EventArgs e) =>
            {
                Button btn = (Button)sender;
                toSpeak = btn.Text;
                if (speaker == null)
                {
                    speaker = new Android.Speech.Tts.TextToSpeech(this, this);
                }
                else
                {
                    var p = new Dictionary <string, string> ();
                    speaker.Speak(toSpeak, QueueMode.Flush, p);
                    System.Diagnostics.Debug.WriteLine("spoke " + toSpeak);
                }
            };
        }
Exemplo n.º 2
0
 public void OnInit(OperationResult status)
 {
     if (status.Equals(OperationResult.Success))
     {
         speaker.SetLanguage(Settings.CurrentPronunciation == $"{Pronunciation.en}" ? Locale.Us : Locale.Uk);
         speaker.Speak(ToSpeak, QueueMode.Flush, null, null);
     }
 }
Exemplo n.º 3
0
        public void Speak(string text)
        {
            _text = text;
            if (!this.IsSpeakerInitialized(speaker))
            {
                speaker = this.InitializingSpeaker();
            }

            speaker.Speak(_text, SpeechTts.QueueMode.Flush, null, null);
        }
Exemplo n.º 4
0
 public void Speak(string text)
 {
     toSpeak = text;
     if (speaker == null)
     {
         speaker = new Android.Speech.Tts.TextToSpeech(MainActivity.Instance, this);
     }
     else
     {
         speaker.Speak(toSpeak, QueueMode.Flush, null, null);
     }
 }
Exemplo n.º 5
0
 public void Speak(string text)
 {
     toSpeak = text;
     if (speaker == null)
     {
         MainActivity context = (MainActivity)Forms.Context;
         speaker = new DroidTts.TextToSpeech(context, this);
     }
     else
     {
         speaker.Speak(toSpeak, DroidTts.QueueMode.Flush, null, null);
     }
 }
Exemplo n.º 6
0
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			//Inicializaremos nuestra variable principal que nos pide dos parametros nuestro contexto y una clase
			//tipo TextToSpeech.IOnInitListener la cual heredamos mediante la implementacion de la clase
			tts = new TextToSpeech (this, this);

			Button button = FindViewById<Button> (Resource.Id.Button1);
			EditText caja = FindViewById<EditText> (Resource.Id.Texto);
			button.Click += delegate {
				//Verificamos que nuestra variable tipo TextToSpeech sea diferente de null
				if (tts!=null) {
					//En una variable tipo string depositamos el contenido de nuestro textbox
					String text = caja.Text;
					//Verificamos que nuestra variable de texto no sea nula para evitar una excepcion
					if (text!=null) {
						//Verificamos que nuestro telefono no este usando la funcion de dictado
						if (!tts.IsSpeaking) {
							//mandamos a llamar la funcion de dictado que nos pedida tres parametros
							//1 Texto a dictar
							//2 Modo
							//3 Diccionario de parametros el cual ira vacio en esta ocacion
							tts.Speak(text, QueueMode.Flush, new Dictionary<string, string>());
						}
					}
				}
			};
		}
Exemplo n.º 7
0
        public void Speak(string text)
        {
            var ctx = Forms.Context; // useful for many Android SDK features

            toSpeak = text;
            if (speaker == null)
            {
                speaker = new Android.Speech.Tts.TextToSpeech(ctx, this);
            }
            else
            {
                var p = new Dictionary <string, string>();
                speaker.Speak(toSpeak, QueueMode.Flush, p);
                Debug.WriteLine("tts android: speak=" + toSpeak);
            }
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        private void Speak()
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }

            if (!queue && textToSpeech.IsSpeaking)
            {
                textToSpeech.Stop();
            }

            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();
            }

            textToSpeech.SetPitch(pitch);
            textToSpeech.SetSpeechRate(speakRate);

            textToSpeech.Speak(text, queue ? QueueMode.Add : QueueMode.Flush, null);
        }
        public async Task SpeakAsync(string text, int max, SpeechOptions options, CancellationToken cancelToken)
        {
            await Initialize();

            // Wait for any previous calls to finish up
            if (tcsUtterances?.Task != null)
            {
                await tcsUtterances.Task;
            }

            if (cancelToken != null)
            {
                cancelToken.Register(() =>
                {
                    try
                    {
                        tts?.Stop();

                        tcsUtterances?.TrySetResult(true);
                    }
                    catch
                    {
                    }
                });
            }

            if (options?.Locale?.Language != null)
            {
                JavaLocale locale = null;
                if (!string.IsNullOrWhiteSpace(options?.Locale.Country))
                {
                    locale = new JavaLocale(options.Locale.Language, options.Locale.Country);
                }
                else
                {
                    locale = new JavaLocale(options.Locale.Language);
                }

                tts.SetLanguage(locale);
            }
            else
            {
                SetDefaultLanguage();
            }

            if (options?.Pitch.HasValue ?? false)
            {
                tts.SetPitch(options.Pitch.Value);
            }
            else
            {
                tts.SetPitch(TextToSpeech.PitchDefault);
            }

            tts.SetSpeechRate(1.0f);

            var parts = text.SplitSpeak(max);

            numExpectedUtterances = parts.Count;
            tcsUtterances         = new TaskCompletionSource <bool>();

            var guid = Guid.NewGuid().ToString();

            for (var i = 0; i < parts.Count && !cancelToken.IsCancellationRequested; i++)
            {
                // We require the utterance id to be set if we want the completed listener to fire
                var map = new Dictionary <string, string>
                {
                    { AndroidTextToSpeech.Engine.KeyParamUtteranceId, $"{guid}.{i}" }
                };

                if (options != null && options.Volume.HasValue)
                {
                    map.Add(AndroidTextToSpeech.Engine.KeyParamVolume, options.Volume.Value.ToString(CultureInfo.InvariantCulture));
                }

                // We use an obsolete overload here so it works on older API levels at runtime
                // Flush on first entry and add (to not flush our own previous) subsequent entries
#pragma warning disable CS0618
                tts.Speak(parts[i], i == 0 ? QueueMode.Flush : QueueMode.Add, map);
#pragma warning restore CS0618
            }

            await tcsUtterances.Task;
        }
		protected override void OnCreate(Bundle bundle)
		{
			base.OnCreate(bundle);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);

			// we need to register the onscreen gadgets we're interested in

			var btnSayIt = FindViewById<Button>(Resource.Id.btnSpeak);
			var editWhatToSay = FindViewById<EditText>(Resource.Id.editSpeech);
			var spinLanguages = FindViewById<Spinner>(Resource.Id.spinLanguage);
			var txtSpeedVal = FindViewById<TextView>(Resource.Id.textSpeed);
			var txtPitchVal = FindViewById<TextView>(Resource.Id.textPitch);
			var seekSpeed = FindViewById<SeekBar>(Resource.Id.seekSpeed);
			var seekPitch = FindViewById<SeekBar>(Resource.Id.seekPitch);

			// set up the initial pitch and speed values then the onscreen values
			// the pitch and rate both go from 0f to 1f, however if you have a seek bar with a max of 1, you get a single step
			// therefore, a simpler option is to have the slider go from 0 to 255 and divide the position of the slider by 255 to get
			// the float
			seekSpeed.Progress = seekPitch.Progress = 127;
			txtSpeedVal.Text = txtPitchVal.Text = "0.5";

			// get the context - easiest way is to obtain it from an on screen gadget
			context = btnSayIt.Context;

			// set up the TextToSpeech object
			// third parameter is the speech engine to use
			textToSpeech = new TextToSpeech(this, this, "com.google.android.tts");

			// set up the langauge spinner
			// set the top option to be default
			var langAvailable = new List<string>{ "Default" };

			// our spinner only wants to contain the languages supported by the tts and ignore the rest
			var localesAvailable = Java.Util.Locale.GetAvailableLocales().ToList();
			foreach (var locale in localesAvailable)
			{
				LanguageAvailableResult res = textToSpeech.IsLanguageAvailable(locale);
				switch (res)
				{
				case LanguageAvailableResult.Available:
					langAvailable.Add(locale.DisplayLanguage);
					break;
				case LanguageAvailableResult.CountryAvailable:
					langAvailable.Add(locale.DisplayLanguage);
					break;
				case LanguageAvailableResult.CountryVarAvailable:
					langAvailable.Add(locale.DisplayLanguage);
					break;
				}

			}
			langAvailable = langAvailable.OrderBy(t => t).Distinct().ToList();

			var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, langAvailable);
			spinLanguages.Adapter = adapter;

			// set up the speech to use the default langauge
			// if a language is not available, then the default language is used.
			lang = Java.Util.Locale.Default;
			textToSpeech.SetLanguage(lang);

			// set the speed and pitch
			textToSpeech.SetPitch(.5f);
			textToSpeech.SetSpeechRate(.5f);

			// connect up the events
			btnSayIt.Click += delegate
			{
				// if there is nothing to say, don't say it
				if (!string.IsNullOrEmpty(editWhatToSay.Text))
					textToSpeech.Speak(editWhatToSay.Text, QueueMode.Flush, null);
			};

			// sliders
			seekPitch.StopTrackingTouch += (object sender, SeekBar.StopTrackingTouchEventArgs e) =>
			{
				var seek = sender as SeekBar;
				var progress = seek.Progress / 255f;
				textToSpeech.SetPitch(progress);
				txtPitchVal.Text = progress.ToString("F2");
			};
			seekSpeed.StopTrackingTouch += (object sender, SeekBar.StopTrackingTouchEventArgs e) =>
			{
				var seek = sender as SeekBar;
				var progress = seek.Progress / 255f;
				textToSpeech.SetSpeechRate(progress);
				txtSpeedVal.Text = progress.ToString("F2");
			};

			spinLanguages.ItemSelected += (object sender, AdapterView.ItemSelectedEventArgs e) =>
			{
				lang = Java.Util.Locale.GetAvailableLocales().FirstOrDefault(t => t.DisplayLanguage == langAvailable[(int)e.Id]);
				// create intent to check the TTS has this language installed
				var checkTTSIntent = new Intent();
				checkTTSIntent.SetAction(TextToSpeech.Engine.ActionCheckTtsData);
				StartActivityForResult(checkTTSIntent, NeedLang);
			};
		}
Exemplo n.º 12
0
        async Task Speak(CancellationToken?cancelToken)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                //return Task.CompletedTask;
                return;
            }

            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();
            var flag = new AsyncManualResetEvent();

            var utteranceID = Guid.NewGuid().ToString();
            var doneTask    = listener.ListenFor(utteranceID);

            cancelToken?.Register(() =>
            {
                textToSpeech.Stop();
                tcs?.TrySetCanceled();
                listener?.completionSources?[utteranceID]?.TrySetCanceled();
            });

            var utteranceProgressListenerDictionary = new Dictionary <string, string>();

            utteranceProgressListenerDictionary.Add(Android.Speech.Tts.TextToSpeech.Engine.KeyParamUtteranceId, utteranceID);
            utteranceProgressListenerDictionary.Add(Android.Speech.Tts.TextToSpeech.Engine.KeyParamVolume, volume.ToString());

            textToSpeech.SetPitch(pitch);
            textToSpeech.SetSpeechRate(speakRate);

            //textToSpeech.SetOnUtteranceProgressListener(new TtsProgressListener(flag));

            //var bundle = new Android.OS.Bundle();
            //bundle.PutFloat(Android.Speech.Tts.TextToSpeech.Engine.KeyParamVolume, 1.0f);

#pragma warning disable CS0618 // Type or member is obsolete
            //textToSpeech.Speak(text, QueueMode.Flush, bundle, text);
            textToSpeech.Speak(text, QueueMode.Flush, utteranceProgressListenerDictionary);
#pragma warning restore CS0618 // Type or member is obsolete

            //return tcs.Task;
            //return Task.WhenAny(tcs.Task, flag.WaitAsync());
            //return listener.IsDone();

            //int i = 0;
            //var sw = new System.Diagnostics.Stopwatch();
            //sw.Start();
            //while (doneTask.Status != TaskStatus.RanToCompletion)
            //{
            //    Task.Delay(100).Wait(101);
            //    //await Task.Delay(100, CancellationTokenHelpers.Timeout(105).Token);
            //    Android.Util.Log.Debug("Speech", $"Iteration {i}, {sw.ElapsedMilliseconds} ms - status is {doneTask.Status}.");
            //    i++;
            //}

            //return doneTask;
            await doneTask;
        }
Exemplo n.º 13
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.Main);

            textToSpeak = FindViewById<EditText> (Resource.Id.textToSpeak);
            speechButton = FindViewById<Button> (Resource.Id.speechButton);
            speechItemListView = FindViewById<ListView> (Resource.Id.speechItemListView);

            speech = new TextToSpeech (this, this);
                        speech.SetLanguage (Java.Util.Locale.Default); //translate to default locale

            speechButton.Click += (object sender, EventArgs e) => {

                string text = textToSpeak.Text;

                if (!String.IsNullOrEmpty (text)) {
                    speech.Speak (text, QueueMode.Add, null);
                } else {
                                        Toast.MakeText (this, Resource.String.enter_text_to_speak, ToastLength.Short).Show ();
                }
            };

            speechItemListView.ChoiceMode = ChoiceMode.Single;

            if (speechItemListView != null) {
                speechItemListView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {
                    textToSpeak.Text = items [e.Position];
                    speechItemListView.SetSelection (e.Position);
                                        this.InvalidateOptionsMenu ();
                };
            }

                        textToSpeak.TextChanged += (sender, e) => this.InvalidateOptionsMenu();
        }