4

我有一个使用 TTS 读取 AsyncTask 中的文本的应用程序。我的问题是当手机进入睡眠状态时播放停止。我在该主题中经历了许多线程,所有内容都指向 WakeLock。但是我无法实现它,无论我是在活动类还是 AsyncTask 类中调用它。我使用以下代码:

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

我想问题出在 TTS 上,但目前我一无所知。也许有更多 TTS 和 WakeLock 经验的人可以帮助我。

提前致谢

编辑:

这是完整的代码(删除了不重要的部分):

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

1 回答 1

3

onPause()当屏幕熄灭并导致 TTS 停止时正在调用。

于 2013-10-30T13:35:31.580 回答