文本到语音在模拟器上有效,但在手机上无效

时间:2013-02-26 10:51:14

标签: android locale text-to-speech

我已经浏览了与此主题相关的几乎所有链接,但未找到任何合适的答案。我正在尝试构建一个基本的文本到语音应用程序。它在模拟器上完美运行。但是当我在手机上运行时,它显示不支持的语言。 我想这与语言环境有关。我手机中设置的默认语言环境为en_US 我的代码如下:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tts = new TextToSpeech(this, this);
    btnSpeak = (Button) findViewById(R.id.btnSpeak);
    txtText = (EditText) findViewById(R.id.txtText);
    btnSpeak.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // Method yet to be defined
            speakOut();
        }

    });
}

public void onInit(int status) {
    // TODO Auto-generated method stub
    // TTS is successfully initialized
    if (status == TextToSpeech.SUCCESS) {
        // Setting speech language
        // System.out.println(Locale.getAvailableLocales());
        int result = tts.setLanguage(Locale.ENGLISH);
        // int result = tts.setLanguage(Locale.getDefault());
        // If your device doesn't support language you set above
        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            // Cook simple toast message with message
            Toast.makeText(this, "Language not supported",
                    Toast.LENGTH_LONG).show();
            Log.e("TTS", "Language is not supported");

        } else {
            btnSpeak.setEnabled(true);
        }
        // TTS is not initialized properly
    } else {
        Toast.makeText(this, "TTS Initilization Failed", Toast.LENGTH_LONG)
                .show();
        Log.e("TTS", "Initilization Failed");
    }
}

private void speakOut() {
    // Get the text typed
    String text = txtText.getText().toString();
    // If no text is typed, tts will read out 'You haven't typed text'
    // else it reads out the text you typed
    if (text.length() == 0) {
        tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH, null);
    } else {
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

}

public void onDestroy() {
    // Don't forget to shutdown!
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}

即使我使用int result = tts.setLanguage(Locale.getDefault());代替Locale.ENGLISH,也会显示相同语言不支持的消息

LOGCAT错误:

02-26 16:24:57.492: I/TextToSpeech.java(23356): initTts() successfully bound to service
02-26 16:24:58.015: E/TTS(23356): Language is not supported

我在手机上运行 - 三星Galaxy-Y(GT-S5360)与Android版本2.3.6

1 个答案:

答案 0 :(得分:0)

自己找到了解决方案。问题是android默认tts引擎上可用的语言环境与我手机上的语言环境不匹配 我安装了另一个tts引擎,它可以很好地完成它。