我一直在网上冲浪(阅读 google/android 开发者文档,阅读 stackoverflow 的类似问题,阅读书籍)并且非常密切地关注示例。文本到语音的“说话”效果很好。但我就是无法调用 OnUtteranceCompleted。
它一定很简单,以至于我没有看到答案。请帮忙!这是我经过几次互动后的代码。
或者有人可以提供一个完整的源代码(而不是片段),以查看它是否适用于我的模拟器/实际设备?
public class testActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener {
...
protected void checkTtS() {
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, TTS_DATA_CHECK_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
....
if (requestCode == TTS_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
mTts = new TextToSpeech(this, this);
....
// Implements TextToSpeech.OnInitListener.
public void onInit(int status) {
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.FRANCE);
result = mTts.setOnUtteranceCompletedListener(this);
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");
mTts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params);
....
public void onUtteranceCompleted(String uttId) {
Toast.makeText(getBaseContext(), "onutterancecompleted", Toast.LENGTH_SHORT).show();
}