0

我一直在网上冲浪(阅读 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();
 }
4

2 回答 2

0

您是否在 setOnUtteranceCompletedListener 调用后检查了 int 结果的值。

if(result == TextToSpeech.ERROR)

然后未设置话语侦听器

于 2011-09-27T14:20:31.093 回答
-1

您的代码可能应该如下所示:

public void onUtteranceCompleted(String uttId) {
    if (uttId.equals("stringId")) {
        Toast.makeText(getBaseContext(), "onutterancecompleted", Toast.LENGTH_SHORT).show();
    }
 }

你也应该看看这篇文章,有一个关于如何onUtteranceCompleted工作的清晰描述。希望这可以帮助。

于 2011-09-27T14:22:03.043 回答