How to know when TTS is finished?

2019-01-07 11:39发布

I'm implementing an Interactive Voice Response application on Android. I would like to know how to determine when the tts.speak() function has done talking so I can call my speech recognizer function.

5条回答
萌系小妹纸
2楼-- · 2019-01-07 11:51
public class TTSActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener, ... {
private TextToSpeech mTts;
...........
private void speak(String text) {
   if(text != null) {
      HashMap<String, String> myHashAlarm = new HashMap<String, String>();
      myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
      myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE");
      mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
   }
}
// Fired after TTS initialization
public void onInit(int status) {
    if(status == TextToSpeech.SUCCESS) {
        mTts.setOnUtteranceCompletedListener(this);
    }
}
// It's callback
public void onUtteranceCompleted(String utteranceId) {
   Log.i(TAG, utteranceId); //utteranceId == "SOME MESSAGE"
   }
...........
}

Read A good tutorial

查看更多
Anthone
3楼-- · 2019-01-07 11:55

use a SpeakableListener when do text to speak.

private volatile boolean finished;

private SpeakableListener listener = new SpeakableListener() {

    @Override
    public void markerReached(SpeakableEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void speakableCancelled(SpeakableEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void speakableEnded(SpeakableEvent arg0) {
        TestSpeaker.this.finished = true;

    }

    @Override
    public void speakablePaused(SpeakableEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void speakableResumed(SpeakableEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void speakableStarted(SpeakableEvent arg0) {
        TestSpeaker.this.finished = false;

    }

    @Override
    public void topOfQueue(SpeakableEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void wordStarted(SpeakableEvent arg0) {
        // TODO Auto-generated method stub

    }
};

/** Speak the following string. */
public void sayIt(String str) {
    System.out.print(" " + str + " ");
    /* The the synthesizer to speak. */
    synthesizer.speakPlainText(str, listener);
查看更多
迷人小祖宗
4楼-- · 2019-01-07 12:00

Try this following code which shows a toast after TTS completed. Replace toast with your own action.

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{


private boolean initialized;
private String queuedText;
private String TAG = "TTS";
private TextToSpeech tts;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    tts = new TextToSpeech(this /* context */, this /* listener */);
    tts.setOnUtteranceProgressListener(mProgressListener);


    speak("hello world");

}




public void speak(String text) {

    if (!initialized) {
        queuedText = text;
        return;
    }
    queuedText = null;

    setTtsListener(); // no longer creates a new UtteranceProgressListener each time
    HashMap<String, String> map = new HashMap<String, String>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
    tts.speak(text, TextToSpeech.QUEUE_ADD, map);
}


private void setTtsListener() {

}





@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        initialized = true;
        tts.setLanguage(Locale.ENGLISH);

        if (queuedText != null) {
            speak(queuedText);
        }
    }
}



private abstract class runnable implements Runnable {
}




private UtteranceProgressListener mProgressListener = new UtteranceProgressListener() {
    @Override
    public void onStart(String utteranceId) {
    } // Do nothing

    @Override
    public void onError(String utteranceId) {
    } // Do nothing.

    @Override
    public void onDone(String utteranceId) {

        new Thread()
        {
            public void run()
            {
                MainActivity.this.runOnUiThread(new runnable()
                {
                    public void run()
                    {

                        Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show();

                    }
                });
            }
        }.start();

    }
}; }
查看更多
Ridiculous、
5楼-- · 2019-01-07 12:01

I noticed that there are people having problems in the use of TextToSpeech to ask that the solution to you

How to know when TTS is finished? without use setOnUtteranceCompletedListener

public void isTTSSpeaking(){

    final Handler h =new Handler();

    Runnable r = new Runnable() {

        public void run() {

            if (!tts.isSpeaking()) {
                onTTSSpeechFinished();
            }

            h.postDelayed(this, 1000);
        }
    };

    h.postDelayed(r, 1000);
}
查看更多
欢心
6楼-- · 2019-01-07 12:10

The setOnUtteranceCompletedListener is deprecated since API level 15. Instead, use setOnUtteranceProgressListener.

I found a code snippet (here) that made it really easy for me to know when text to speech is finished:

@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onDone(String utteranceId) {
                // Log.d("MainActivity", "TTS finished");
            }

            @Override
            public void onError(String utteranceId) {
            }

            @Override
            public void onStart(String utteranceId) {
            }
        });
    } else {
        Log.e("MainActivity", "Initilization Failed!");
    }
}

http://www.codota.com/android/scenarios/52fcbd34da0ae25e0f855408/android.speech.tts.TextToSpeech?tag=dragonfly

查看更多
登录 后发表回答