我正在翻译应用程序,将英语翻译成意大利语,意大利语翻译成英语。当输入英文单词时,它被翻译,TTS用本地口音说出翻译。所以我进入"奶酪"这被翻译成意大利语,所以" Formaggio"显示并用意大利口音说。
我的问题是TTS引擎在活动初始化时设置了一个区域设置,我正在努力在运行时在本地语言之间进行更改。如果我输入一个英语单词,它会根据需要使用意大利语语言环境,但如果我输入意大利语单词,它将继续使用第一个使用的语言环境,意大利语不是英语。这也是另一种方式,输入意大利语首先使用英语,但不会切换。
是否可以通过按下按钮设置TTS对象语言以备即时使用?
这是我的代码简化:
public class TranslateActivity extends Activity implements OnInitListener {
Locale locLanguage, locLanguage2;
private TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.translate);
locLanguage = new Locale("gb_GB");
locLanguage2 = new Locale("it_IT");
engtoita = (LinearLayout) findViewById(R.id.engtoitaButton);
itatoeng = (LinearLayout) findViewById(R.id.itatoengButton);
engtoita.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tts.setLanguage(locLanguage2);
class bgStuff extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
phraseEntered = et.getText().toString();
translatedText = translate(phraseEntered);
if(translatedText!="")
{
speakOut(translatedText);
}
} catch (Exception e) {
e.printStackTrace();
z = "Sorry, there was an error.";
translatedText = z;
}
return null;
}
}
}
});
itatoeng.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tts.setLanguage(locLanguage1);
class bgStuff extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
phraseEntered = et.getText().toString();
translatedText = translate(phraseEntered);
if(translatedText!="")
{
speakOut(translatedText);
}
} catch (Exception e) {
e.printStackTrace();
z = "Sorry, there was an error.";
translatedText = z;
}
return null;
}
}
}
});
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(locLanguage);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut(String text) {
tts.speak(text, 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) {
tts = new TextToSpeech(this, this);
} else {
Intent installTTSIntent = new Intent();
installTTSIntent
.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
ArrayList<String> languages = new ArrayList<String>();
languages.add("ita");
installTTSIntent.putStringArrayListExtra(
TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR,
languages);
startActivity(installTTSIntent);
}
}
}
}
我希望这足以代码来展示我想要实现的目标。我已经查找了如何动态设置TTS对象的语言,但看不到任何需要这样的示例。
答案 0 :(得分:2)
迟到总比不上......
你的onActivityResult过于复杂 考虑使用这个:
@Override
protected final void onActivityResult
(
final int requestCode, final int resultCode, final Intent data
)
{
// If TTS Voice data doesn't exist, download it.
if (requestCode == TTS_DATA_CHECK)
{
if (resultCode != Engine.CHECK_VOICE_DATA_PASS)
{
// Voice data doesn't exist
final Intent tnt = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
startActivity(tnt);
}
}
}
(你需要定义这个常量)
private static final int TTS_DATA_CHECK = 1;
设置不同的语言:
// Change language
tts.setLanguage(new Locale("en")); // or "it"
// Say it
tts.speak("All you need is Love!", TextToSpeech.QUEUE_FLUSH, null);
Make sure your device isn't set to "Always use default settings"
,或者不会应用语言更改。
最后注意事项:正如您所猜测的,我的TTS对象名为 tts