Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid TTS + WakeLock
    text
    copied!<p>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:</p> <pre><code>PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My wakelook"); wakeLock.acquire(1000); </code></pre> <p>I guess the problem is with TTS, but currently I am clueless. Maybe someone with more TTS and WakeLock experience could help me out.</p> <h2>Thanks in advance</h2> <p>EDIT:</p> <p>Here is the full code (unimportant parts removed):</p> <pre><code> 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&lt;TTSPlayItem&gt; playItems = new ArrayList&lt;TTSPlayItem&gt;(); 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&lt;TTSPlayItem, TTSPlayItem, String&gt; { // 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 &lt; 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(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload