Fel*_*eap 34 android text-to-speech
我正在Android上实现交互式语音应答应用程序.我想知道如何确定tts.speak()函数何时完成说话,以便我可以调用我的语音识别器功能.
小智 45
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"
}
...........
}
Run Code Online (Sandbox Code Playgroud)
阅读 一个很好的教程
kev*_*ber 17
自API级别15以来,不推荐使用setOnUtteranceCompletedListener.而是使用setOnUtteranceProgressListener.
我找到了一个代码片段(这里),让我很容易知道文本到语音的完成时间:
@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!");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
要知道,当TTS完成后,你必须调用setOnUtteranceProgressListener其中有3种回调方法onStart,onDone并onError
随后包括话语ID给speak方法
代码片段
textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS){
int result=textToSpeech.setLanguage(Locale.ENGLISH);
if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
Log.i("TextToSpeech","Language Not Supported");
}
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.i("TextToSpeech","On Start");
}
@Override
public void onDone(String utteranceId) {
Log.i("TextToSpeech","On Done");
}
@Override
public void onError(String utteranceId) {
Log.i("TextToSpeech","On Error");
}
});
}else {
Log.i("TextToSpeech","Initialization Failed");
}
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我注意到有人在使用TextToSpeech时遇到问题,请问您的解决方案
如何知道TTS完成时间?不使用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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31885 次 |
| 最近记录: |