0

Well... Here is the background:

I'm working in a experimental app that uses pocketsphinx. Basically, I'm running one service that runs a thread. This thread manages a RecognizerTask object. It gives a signal to the RecognizerTask object to start recording and recognizing the speech and after 15 seconds, it signals shutdown to the RecognizerTask object. Right after, it signals start again and the procedure repeats.

Follows the code of the thread that manages the RecognizerTask object:

public void run() {
        while(!this.interrupted){
            try{
                Thread.sleep(15000);
            }
            catch (InterruptedException e){
                e.printStackTrace();
                return;
            }
            rec.shutdown();
            try {
                rec_thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(Thread.interrupted())
                return;

            rec = new RecognizerTask();
            rec_thread = new Thread(rec);

            System.gc();

            rec.setRecognitionListener(MyService.this);

            rec_thread.start();
            rec.start();
        }
}

The problem in here is the following: If the device is awake and the screen, turned on, everything runs fine. But if the screen is locked, the thread just freezes at the .join() call. Inspecting further, I realized that the .join() freezes because one call to the AudioRecord instance that exists inside the ReconizerTask object just does not return! And as soon as I unlock the phone, everything runs fine again! Also, my service acquire a Partial Awake Lock, while it's running.

And worse: this only happens with Galaxy S3 devices (the virtual devices and a Galaxy Nexus didn't show this behavior), as far as I know.

So, the question is: what can be causing this problem?

igor.araujo
  • 1,007
  • 1
  • 7
  • 15
  • 1
    See for details http://stackoverflow.com/questions/4139313/android-service-stops-working-when-phone-locked – Nikolay Shmyrev Jun 01 '13 at 16:13
  • @Nikolay, thanks! This definitively helps. But as I said, this is an experiment. I'm struggling to find out why this behavior only appears in this particular device. – igor.araujo Jun 01 '13 at 17:28
  • @NikolayShmyrev, I substituted the thread for a BroadcastReceiver and used the AlarmManager. The problem is the same: the RecognizerTask thread.join simply does not return while the screen is locked. – igor.araujo Jun 02 '13 at 03:56
  • There is a separate audio thread which recognizer thread runs. join doesn't return because audio thread is locked. You need to make sure that audio thread never lock with the information from the answer above. – Nikolay Shmyrev Jun 02 '13 at 19:32
  • @NikolayShmyrev I didn't fully understand what exactly you mean... But I'm sure that what is preventing the join from returning is the AudioRecord.stop() inside the inner class AudioTask that exists inside the RecognizerTask class. The whole problem lies in the fact that with some devices, AudioRecord.stop() runs as expected and with others (Galaxy S3) it hangs the thread execution (when the screen is locked). – igor.araujo Jun 03 '13 at 06:53
  • It's perfectly fine for some devices to behave differently as long as you are using not very common approaches like running several tasks one from the other. S3 probably decided to optimize something and that lead to an issue in corner case. The easiest way to solve the problem is to make application model more flat and predictable by simplifying threading model and reducing the amount of tasks you embed one into other. – Nikolay Shmyrev Jun 03 '13 at 21:05

0 Answers0