Android Xamarin C# - 如何防止CPU减速

时间:2017-07-12 23:31:46

标签: c# android xamarin

我正在用文字对话来做外语课。我的应用程序说一个英文单词,等待4秒钟,然后说出一个外来词,等待4秒钟并重复该过程。我发现在大约300个单词之后,单词之间的等待时间从4秒到15秒或更长。这是非常令人沮丧的,我已经尝试使用PowerManager来保持cpu,有没有其他方法可以防止这种情况?

以下是一些代码:

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        audioManager = (AudioManager)GetSystemService(AudioService);
        SetContentView(Resource.Layout.Main);
        Button button = FindViewById<Button>(Resource.Id.MyButton);
        Button button2 = FindViewById<Button>(Resource.Id.button1);
        Button button3 = FindViewById<Button>(Resource.Id.button2);
        SpeechText = new TextToSpeech(this, this);
        SpeechText.SetSpeechRate(0.7f);
        //...
        button3.Click += delegate { StartLessons(); };
        PowerManager pm = (PowerManager)GetSystemService(Context.PowerService);
        mWakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "PartialWakeLockTag");

    }
public void StartLessons()
    {
        if (ThreadStarted == true)
        {
            TV.Text = "Lessons already started.";
            return;
        }
        StopBH();
        var focusResult = audioManager.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain);
        if (focusResult != AudioFocusRequest.Granted)
        {
            TV.Text = "Audio Focus not granted.";
            return;
        }            
        StopLessons = false;            
        lessoncount = 0;
        ThreadPool.QueueUserWorkItem(o => sl.SpeakLessonsBackground());
        TV.Text = "Lessons Started.";
    }
    class SL
    {
        public async void SpeakLessonsBackground()
        {
            if (mWakeLock.IsHeld == false)
            {
                mWakeLock.Acquire();                    
            }
            int r;
            string first, second, term;
            while (StopLessons == false)
            {
                ThreadStarted = true;
                r = sentence_generator.RandomNumber.Rand4(CleanRosetta.Count());                    
                term = CleanRosetta[r];
                first = term.Substring(term.IndexOf('*') + 2);
                second = term.Substring(0, term.IndexOf('*') - 1);                                      
                wordcount++;
                if (wordcount > 10)
                {
                    wordcount = 0;
                    RandomizeCleanRosetta();
                }
                SpeechText.Speak(first, QueueMode.Flush, null, "LessonUtterance");
                while (SpeechText.IsSpeaking != true)
                {
                    await Task.Delay(25);
                    if (StopLessons == true)
                    {
                        EndSL();
                        return;
                    }
                }
                while (SpeechText.IsSpeaking == true)
                {
                    await Task.Delay(25);
                    if (StopLessons == true)
                    {
                        EndSL();
                        return;
                    }
                }                    
                var dt = DateTime.Now;
                dt = dt.AddSeconds(4);
                while (DateTime.Now < dt)
                {                        
                    await Task.Delay(25);
                    if (StopLessons == true)
                    {
                        EndSL();
                        return;
                    }
                }
                SpeechText.Speak(second, QueueMode.Flush, null, "LessonUtteranceEnd");
                while (SpeechText.IsSpeaking != true)
                {
                    await Task.Delay(25);
                    if (StopLessons == true)
                    {
                        EndSL();
                        return;
                    }
                }
                while (SpeechText.IsSpeaking == true)
                {
                    await Task.Delay(25);
                    if (StopLessons == true)
                    {
                        EndSL();
                        return;
                    }
                }                    
                dt = DateTime.Now;
                dt = dt.AddSeconds(4);
                while (DateTime.Now < dt)
                {                        
                    await Task.Delay(25);
                    if (StopLessons == true)
                    {
                        EndSL();
                        return;
                    }
                }
                lessoncount++;                    
            }
            EndSL();
        }
    }

    public static void EndSL()
    {
        StopLessons = false;
        ThreadStarted = false;
        if (mWakeLock.IsHeld == true)
        {
            mWakeLock.Release();                
        }
    }

0 个答案:

没有答案