我想做一个文本到语音的测试应用程序,看一下这个教程:http://android-developers.blogspot.com.es/2009/09/introduction-to-text-to-speech-in.html
我试着按照教程说的做,并且我只将语言设置为西班牙语:
mTts = new TextToSpeech(this, this);
Locale loc = new Locale ("spa", "ESP");
mTts.setLanguage(loc);我也试过这样做(不管用):
// Specify the exact voice you are checking for
checkIntent.putExtra(TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR,"spa-ESP");问题是,当应用程序启动时,它会提示我进入一个屏幕,要求我下载四种语言:德语、西班牙语、法语和意大利语。如果我只下载西班牙语,然后按Back键,应用程序就可以工作了,但每次我启动应用程序时,它都会显示相同的屏幕来下载其他三种语言,并且只有当我下载foru语言时才会停止显示。
我的目标是提示用户只下载西班牙语,并且只显示一次语言下载对话框,而不是一直显示。
我在一部带有4.1版本的Nexus S手机上测试它
这是完整的代码:
public class MainActivity extends Activity implements OnInitListener {
private static final int MY_DATA_CHECK_CODE = 0;
EditText editText;
Button button;
private TextToSpeech mTts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
editText.setText("Estoy probando, porque hay que probar.");
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTts.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
});
}
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);
Locale loc = new Locale ("spa", "ESP");
mTts.setLanguage(loc);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
mTts = new TextToSpeech(this, this);
Locale loc = new Locale ("spa", "ESP");
mTts.setLanguage(loc);
}
}
}
@Override
public void onInit(int arg0) {}
}我希望你能帮我找到一个解决方案。
问候
发布于 2013-03-13 23:11:23
您不能使用MARC codes而必须使用ISO-639-1 / ISO-3166-1代码来创建语言环境:Locale loc = new Locale ("es");。另请参阅Locale class reference中的“类概述”部分。
https://stackoverflow.com/questions/14871443
复制相似问题