Ben*_*ler 7 android text-to-speech google-text-to-speech
我的应用程序被视障人士使用,因此它严重依赖于文本到语音.应用程序调用API并向用户读取加载(使用android.speech.tts.TextToSpeech)一些相关信息.
一切都很好,除了我注意到有时文本到语音的初始化需要10秒或更长时间,这是我的应用程序的主要瓶颈.
我想知道是否有人对如何优化我的代码以缓解此问题有任何想法.
首先,我的应用程序启动一项活动来检查TTS数据.
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
Run Code Online (Sandbox Code Playgroud)
然后,使用活动的结果调用此方法.根据结果,文本到语音只是初始化(这几乎总是发生)或文本到语音安装在设备上(非常罕见).
private TextToSpeech mTts;
@Override
protected 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后,当所有这一切完成后,我的应用程序调用了一个API,并且有几行如下:
mTts.speak("<Useful output here>", TextToSpeech.QUEUE_FLUSH, null);
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!