其他分享
首页 > 其他分享> > 从广播接收器或服务启动Android TTS

从广播接收器或服务启动Android TTS

作者:互联网

我想让TTS在后台运行.但是,我从来没有听到任何声音.我有一个广播接收器启动服务.我把我的TTS代码放在这两个代码中,但它从不说话.我知道正在调用该方法(我在其上设置了一个断点),但它仍然不起作用.

这是我的日志,但似乎没有包含有关TTS服务的任何内容.

10-04 22:45:30.663: WARN/InputManagerService(209): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4423df40
10-04 22:45:37.363: INFO/PollingManager(449): calculateShortestInterval(): shortest interval is 540000
10-04 22:45:37.413: INFO/TLSStateManager(449): org.apache.harmony.nio.internal.SocketChannelImpl@4400ece0: Wrote out 29 bytes of data with 0 bytes remaining.
10-04 22:45:38.043: ERROR/IMAPEmailService(480): Can't create default IMAP system folder Trash. Please reconfigure the folder names.
10-04 22:45:40.123: ERROR/EONS(303): EF_PNN: No short Name
10-04 22:45:41.543: ERROR/WMSTS(171): Month is invalid: 0
10-04 22:45:42.043: WARN/AudioFlinger(172): write blocked for 212 msecs, 24 delayed writes, thread 0xb998

提前谢谢大家!

解决方法:

有助于查看您的TTS代码,以便人们更轻松地为您提供帮助.由于我已经在BroadcastReceiver中使用了TTS,这里是一个从我的代码中删除的示例.

public static class TTS extends Service implements TextToSpeech.OnInitListener, OnUtteranceCompletedListener {
    private TextToSpeech mTts;
    private String spokenText;

    @Override
    public void onCreate() {
        mTts = new TextToSpeech(this, this);
        // This is a good place to set spokenText
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = mTts.setLanguage(Locale.US);
            if (result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED) {
                mTts.speak(spokenText, TextToSpeech.QUEUE_FLUSH, null);
            }
        }
    }

    @Override
    public void onUtteranceCompleted(String uttId) {
        stopSelf();
    }

    @Override
    public void onDestroy() {
        if (mTts != null) {
            mTts.stop();
            mTts.shutdown();
        }
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

在BroadcastReceiver中您希望它发言的位置启动TTS服务:

context.startService(new Intent(context, TTS.class));

如果不是提问者,我希望这可以帮助别人(我确信他现在已经开始工作了).

标签:android,android-service,text-to-speech
来源: https://codeday.me/bug/20191006/1858809.html