1

我浏览了几乎所有与该主题相关的链接,但没有找到任何合适的答案。我正在尝试构建一个基本的文本到语音应用程序。它在模拟器上工作得很好。但是当我在手机上运行它时,它显示不支持的语言。我想这与语言环境有关。我的手机中设置的默认语言环境是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

4

1 回答 1

1

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

于 2013-03-08T07:48:54.950 回答