Looper线程问题?但不使用Animator

时间:2018-05-10 03:22:02

标签: android xamarin text-to-speech

我正在运行SynthesizeToFile的循环(在此示例中,我将循环运行10次)

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初始化

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

        }       

我正在设置SetOnUtteranceProgressListenerSetOnUtteranceProgressListener1,这是以下

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

每当它为OnDone时,我调用一个基本上只检查它是否为第10个计数的函数,意味着这是要处理的最后一个文件并使某些控件可见,如下面的

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

        }

我得到的错误是以下

  

动画师只能在Looper线程上运行

当它到达FloatingActionButton FAB = FindViewById<FloatingActionButton>(Resource.Id.fab);

我也尝试将其作为全局变量,但仍然没有运气。

我认为我搞糟了,不知道该怎么办?

1 个答案:

答案 0 :(得分:2)

将您的UI更改放在RunOnUithread操作:

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