Example #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            AppController.CheckStrictMode();

            base.OnCreate(savedInstanceState);

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

            btnSayIt      = FindViewById <Button>(Resource.Id.btnSpeak);
            btnShare      = FindViewById <Button>(Resource.Id.btnShare);
            btnClear      = FindViewById <Button>(Resource.Id.btnClear);
            cbShareText   = FindViewById <CheckBox>(Resource.Id.checkBoxShareText);
            spinLanguages = FindViewById <Spinner>(Resource.Id.spinLanguage);

            var editWhatToSay = FindViewById <EditText>(Resource.Id.editSpeech);
            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);
            var lblVersion    = FindViewById <TextView>(Resource.Id.lblVersion);

            // create the TTS callback handler
            listner = new UtteranceProgressListenerWrapper(
                (string x) =>
            {
                //onDone
                saveDone = true;
                return(true);
            },
                (string x) =>
            {
                //onError
                saveDone = false;
                AlertDialog.InfoMessage(this, ":(", GetText(Resource.String.say_error), null);
                return(false);
            },
                (string x) =>
            {
                //onBegin
                saveDone = false;
                return(true);
            }
                );

            controller = new AppController(this, this, this, listner);

            // 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
            seekPitch.Progress = 254;
            seekSpeed.Progress = 254;
            txtPitchVal.Text   = "1.00";
            txtSpeedVal.Text   = "1.00";

            // connect up the events
            btnSayIt.Click += delegate
            {
                // if there is nothing to say, don't say it
                if (!string.IsNullOrEmpty(editWhatToSay.Text))
                {
                    controller.Speak(editWhatToSay.Text);
                }
                else
                {
                    Toast.MakeText(this, GetText(Resource.String.no_text), ToastLength.Long).Show();
                }
                saveDone = false;
            };

            btnClear.Click += delegate
            {
                editWhatToSay.Text = "";
                saveDone           = false;
            };

            btnShare.Click += delegate
            {
                SaveAudio(editWhatToSay.Text);
            };

            // sliders
            seekPitch.StopTrackingTouch += (object sender, SeekBar.StopTrackingTouchEventArgs e) =>
            {
                var seek     = sender as SeekBar;
                var progress = seek.Progress / 255f;

                controller.SetPitch(progress);

                txtPitchVal.Text = progress.ToString("F2");
            };

            seekSpeed.StopTrackingTouch += (object sender, SeekBar.StopTrackingTouchEventArgs e) =>
            {
                var seek     = sender as SeekBar;
                var progress = seek.Progress / 255f;

                controller.SetSpeechRate(progress);

                txtSpeedVal.Text = progress.ToString("F2");
            };

            spinLanguages.ItemSelected += (object sender, AdapterView.ItemSelectedEventArgs e) =>
            {
                // find the selected locale
                controller.UpdateLocale((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);
            };

            lblVersion.Text = "v." + PackageManager.GetPackageInfo(PackageName, 0).VersionName;
        }
        public AppController(Context context, Activity activity, TextToSpeech.IOnInitListener initListener, UtteranceProgressListenerWrapper listenerWrapper)
        {
            _context  = context;
            _activity = activity;

            OutputFile = new OutputFileModel(_context.GetText(Resource.String.save_dir));

            SetDefaultLocale();

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

            // set up the speech to use the default langauge
            // if a language is not available, then the default language is used.
            _textToSpeech.SetLanguage(SelectedLocale);

            // set the speed and pitch
            _textToSpeech.SetPitch(1f);
            _textToSpeech.SetSpeechRate(1f);

            _textToSpeech.SetOnUtteranceProgressListener(listenerWrapper);
        }