Android TTS WakeLock

Android TTS WakeLock,第1张

概述我有一个使用TTS读取AsyncTask中的文本的应用程序.我的问题是当手机进入睡眠状态时播放停止.我在主题中经历了很多线索,一切都指向了WakeLock.但是,无论我是在activity类还是AsyncTask类中调用它,我都无法实现它.我使用以下代码: PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE 我有一个使用TTS读取AsyncTask中的文本的应用程序.我的问题是当手机进入睡眠状态时播放停止.我在主题中经历了很多线索,一切都指向了WakeLock.但是,无论我是在activity类还是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();    }}
解决方法 当屏幕熄灭时调用onPause()并导致TTS停止. 总结

以上是内存溢出为你收集整理的Android TTS WakeLock全部内容,希望文章能够帮你解决Android TTS WakeLock所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1127481.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存