我正在开发android的TextToSpeech引擎。初始化代码是
TextToSpeech mTTS;
mTTS=new TextToSpeech(this, this, "android.speech.tts");
mTTS.setEngineByPackageName("android.speech.tts");
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
但是这个代码是我手机上的一个选择器对话框,用于选择b / w Google的TextToSpeech Engine或Samsung TextToSpeech Engine。现在我要删除此
选择框并直接加载Google的TTS引擎,无需用户互动。请帮助我被困:(
答案 0 :(得分:1)
使用Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA)我相信您正在尝试检查设备上是否安装了TTS数据。顺便说一句,这将检查默认设备语言,如果没有安装,它会将 resultCode 作为 TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA onActivityResult()。
请在下面找到初始化TTS和处理错误的正确方法。
system = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
system.setRecognitionListener(this);
speech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
result = speech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not available, attempting download");
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
else {
Log.e("TTS", "Initialization Failed!");
}
}
}, "com.google.android.tts");
请注意3点:
使用此方法,您将无法获得对话框的任何弹出窗口,并且将初始化tts。让我知道它是否有帮助!