Question

I have an app which is using TTS to read text in an AsyncTask. My problem is when the phone goes to sleep the playback stops. I went through many threads in the topic and everything points towards WakeLock. However I am not able to implement it, no matter if I call it in the activity class or the AsyncTask class. I use the following code:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My wakelook");
wakeLock.acquire(1000);

I guess the problem is with TTS, but currently I am clueless. Maybe someone with more TTS and WakeLock experience could help me out.

Thanks in advance

EDIT:

Here is the full code (unimportant parts removed):

    public class PlayerActivity extends Activity implements
        TextToSpeech.OnInitListener {

    // TTS fields
    private TextToSpeech mTts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);

        List<TTSPlayItem> playItems = new ArrayList<TTSPlayItem>();


        TTSPlayItem playItem = new TTSPlayItem();
        playItem.locale = getLocale1();
        playItem.text = getText1();
        playItem.position = position;
        playItems.add(playItem);
        playItem = new TTSPlayItem();
        playItem.locale = getLocale2();
        playItem.text = getText2();
        playItem.position = position;
        playItems.add(playItem);

        TTSPlayItem[] passPlayItems = playItems
                .toArray(new TTSPlayItem[playItems.size()]);
        TTSAsyncTask speak = new TTSAsyncTask();
        speak.execute(passPlayItems);
    }

    /*
     * AsyncTask for TTS
     */

    private class TTSAsyncTask extends
            AsyncTask<TTSPlayItem, TTSPlayItem, String> {

        // WakeLock
        PowerManager pm;
        PowerManager.WakeLock wakeLock;

        protected void onPreExecute() {
        }

        @Override
        protected String doInBackground(TTSPlayItem... items) {
            pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My wakelook");

            wakeLock.acquire();

            for (int i = 0; i < items.length; i++) {
                TTSPlayItem[] progressList = new TTSPlayItem[1];
                progressList[0] = items[i];
                publishProgress(progressList);
                Log.i(TAG, "Play - locale: " + items[i].locale.toString()
                        + ", text: " + items[i].text);

                int treshold = 0;
                while (true) {
                    int result = mTts.setLanguage(items[i].locale);
                    Log.i(TAG, "Locale return: " + result);
                    if (result == 1)
                        break;
                    if (treshold == 100)
                        break;
                    treshold++;
                }

                mTts.speak(items[i].text, TextToSpeech.QUEUE_FLUSH, null);
                while (mTts.isSpeaking()) {
                    if (playing == false) {
                        mTts.stop();
                        return "Playback stopped.";
                    }
                }

                // wait
                android.os.SystemClock.sleep(1000);
            }
            playing = false;

            if (wakeLock.isHeld())
                    wakeLock.release();

            return "Played list of " + items.length + " items.";
        }

        protected void onProgressUpdate(TTSPlayItem... result) {
        }

        protected void onPostExecute(String result) {
    }

    /*
     * TTS methods
     */

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
                mTts = new TextToSpeech(this, this);
            } else {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent
                        .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }

    public void shutdownTTS() {
        playing = false;
        // Don't forget to shutdown!
        if (mTts != null) {
            mTts.stop();
            mTts.shutdown();
        }
    }

    @Override
    public void onDestroy() {
        shutdownTTS();
        super.onDestroy();
    }

    public void onStop() {
        shutdownTTS();
        super.onStop();
    }

    public void onPause() {
        shutdownTTS();
        super.onPause();
    }
}
Was it helpful?

Solution

onPause() is being called when the screen goes off and it causes the TTS to stop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top