Search code examples
androidxamarintext-to-speech

Looper threads issue? But not using Animator


I am running a loop to SynthesizeToFile (in this example I will run the loop 10 times)

for(int i = 0; i< 10; i++)
{
    textToSpeech.SynthesizeToFile("SOME TEXT", null, new Java.IO.File(System.IO.Path.Combine(documentsPath, i.ToString() + "_audio.wav")), i.ToString());
}

TTS Initialising

void TextToSpeech.IOnInitListener.OnInit(OperationResult status)
        {
            try
            {
                Voice voiceobj = new Voice("en-us-x-sfg#female_2-local", Java.Util.Locale.English, VoiceQuality.VeryHigh, VoiceLatency.VeryHigh, false, null);
                textToSpeech.SetVoice(voiceobj);


                if (status == OperationResult.Error)
                {
                    textToSpeech.SetLanguage(Java.Util.Locale.Default);
                }

                if (status == OperationResult.Success)
                {
                    textToSpeech.SetLanguage(Java.Util.Locale.English);
                    textToSpeech.SetOnUtteranceProgressListener(new UtteranceProgressListener1(this));
                    Log.Info("101029", "Initialised successfully");
                }
            }
            catch (Exception X)
            {
                Log.Info("101028", "TTF Error? " + X.Message);
            }

        }       

I am setting up SetOnUtteranceProgressListener to SetOnUtteranceProgressListener1 which is the following

public class UtteranceProgressListener1 : UtteranceProgressListener
        {
            ReadArticle _parent;
            int total = 0;

            public UtteranceProgressListener1(ReadArticle p_parent)
            {
                _parent = p_parent;
            }

            public override void OnStart(String utteranceId)
            {

            }

            public override void OnError(String utteranceId)
            {
                Log.Info("101029", "OnError called");
            }

            public override void OnDone(String utteranceId)
            {
                Log.Info("101029", "OnDone: " + total.ToString());
                total++;
                _parent.AudioConvertionResults(total);
            }
        }

Every time when it is OnDone I call a function which basically just checks if it a 10th count, means this is the last file to process and make some controls visible such as following

    public bool AudioConvertionResults(int CompInt)
            {
                Log.Info("101029", "ComInt: " + CompInt.ToString() );

                if (CompInt >= 10)
                {
                    try
                    {                  
                        Log.Info("101029", "Triggered at " + CompInt.ToString());
                FloatingActionButton FAB = FindViewById<FloatingActionButton>(Resource.Id.fab);
                    FAB.Visibility = ViewStates.Visible;
                    }
                    catch(Exception X)
                    {
                        Log.Info("101029", "Error Triggering? "+X.Message);
                    return fasle;
                    }
                retuen true;

                }
                else
                {
                    Log.Info("101029", "DID NOT Trigger at " + 
                CompInt.ToString());
                return fasle;
                }

        }

The error I get is the following

Animators may only be run on Looper threads

when it hits the FloatingActionButton FAB = FindViewById<FloatingActionButton>(Resource.Id.fab);

I also tried to make this a global variable but still no luck.

I think I am messing up something bad, any idea what should I do?


Solution

  • Place your UI changes within a RunOnUithread Action:

    ie.

    RunOnUiThread(() => 
    {
        FloatingActionButton FAB = FindViewById<FloatingActionButton>(Resource.Id.fab);
        FAB.Visibility = ViewStates.Visible;
    });