2

在我的申请中,我没有听到任何声音

这是我的代码,

public class AndroidTextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener { /** 在首次创建活动时调用。*/

private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tts = new TextToSpeech(this, this);

    btnSpeak = (Button) findViewById(R.id.btnSpeak);

    txtText = (EditText) findViewById(R.id.txtText);

    // button on click event
    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            speakOut();
        }

    });
}

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

@Override
public void onInit(int status) {
    // TODO Auto-generated method stub

    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.US);

        // tts.setPitch(5); // set pitch level

        // tts.setSpeechRate(2); // set speech speed rate

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "Language is not supported");
        } else {
            btnSpeak.setEnabled(true);
            speakOut();
        }

    } else {
        Log.e("TTS", "Initilization Failed");
    }

}

private void speakOut() {

    String text = txtText.getText().toString();

    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

}

}

我在日志猫中遇到错误

不支持语言

有人帮我吗??我需要在说话时突出显示单词

4

1 回答 1

2

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

或者

 // missing data, install it
    Intent installIntent = new Intent();
    installIntent.setAction(
    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
    startActivity(installIntent);

参考:

于 2013-05-15T04:43:29.873 回答