android TextToSpeech.LANG_AVAILABLE
我还在为我的 tts 苦苦挣扎。在我的模拟器中一切都运行良好,但由于我的手机是瑞典的,我需要检查 Locale.US(我的文本是英文)并安装它。 而且..这就是我认为我所做的,但区域设置不可用并返回-1?我做错了什么吗,我以为英语总是可用的?
public void onInit(int status) {
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS) {
// Set preferred language to US english.
// Note that a language may not be available, and the result will indicate this.
int result = mtts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Lanuage data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
//install it?
result = mtts.isLanguageAvailable(Locale.US);
if (result == TextToSpeech.LANG_AVAILABLE) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Language missing, install English speech?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//installera
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
}
//not installable
Log.e(TAG, "Language not installable");
} else {
// The TTS engine has been successfully initialized.
speak();
}
} else {
// Initialization failed.
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}
I'm still struggling with my tts. Everything works nicely in my emulator, but since my phone is swedish I need to check for the Locale.US (my texts are in english) and install it.
And.. that's what I thought I did, but the Locale is unavailable and returns -1? Am I doing something wrong, I thought the english languages were always available?
public void onInit(int status) {
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS) {
// Set preferred language to US english.
// Note that a language may not be available, and the result will indicate this.
int result = mtts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Lanuage data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
//install it?
result = mtts.isLanguageAvailable(Locale.US);
if (result == TextToSpeech.LANG_AVAILABLE) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Language missing, install English speech?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//installera
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
}
//not installable
Log.e(TAG, "Language not installable");
} else {
// The TTS engine has been successfully initialized.
speak();
}
} else {
// Initialization failed.
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎可以工作..如果手机未连接到计算机。对此一无所知,但是当我断开连接时,我再次运行我的应用程序,它获取了语言包并安装了它,现在它可以流利地讲英语了。
Seem to work.. if the phone isn't connected to the computer. Had no clue about that, but when i disconnected I ran my app again, and it fetched the language pack and installed it and now it speaks english fluently..
你有一个小错误。
而不是 :
你应该有:
然后对话框将显示
you had a small bug.
instead of :
you should have:
then the dialog will show