gpt4 book ai didi

java - 如何阻止 SpeechSynthesis.speak(String) ?

转载 作者:行者123 更新时间:2023-12-02 07:55:09 26 4
gpt4 key购买 nike

 public boolean playing=false;
private void prepareTTSEngine() {
try {
synthesis = SpeechSynthesis.getInstance(this);


synthesis.setSpeechSynthesisEvent(new SpeechSynthesisEvent() {

public void onPlaySuccessful() {
Log.i(TAG, "onPlaySuccessful");
playing=true;

}

public void onPlayStopped() {
Log.i(TAG, "onPlayStopped");
playing=false;

}

public void onPlayFailed(Exception e) {
Log.e(TAG, "onPlayFailed");
e.printStackTrace();
playing=false;
}

public void onPlayStart() {
Log.i(TAG, "onPlayStart");
//playing=true;

}

@Override
public void onPlayCanceled() {
Log.i(TAG, "onPlayCanceled");
playing=false;
}


});

//synthesis.setVoiceType("usenglishfemale1"); // All the values available to you can be found in the developer portal under your account

} catch (InvalidApiKeyException e) {
Log.e(TAG, "Invalid API key\n" + e.getStackTrace());
Toast.makeText(_context, "ERROR: Invalid API key", Toast.LENGTH_LONG).show();
}

}

prepareTTSEngine();
String speech= MapLocationOverlay.mSelectedMapLocation.getShortDesc();
synthesis.setVoiceType("usenglishfemale");

BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.US);
iterator.setText(speech);
int start = iterator.first();

for (int end = iterator.next();end != BreakIterator.DONE; start = end, end = iterator.next()) {
if(speech !=null && speech.substring(start,end).compareTo("")!=0)
{
String sentence_next=speech.substring(start,end);
//set ready flag to true (so isReady returns true)


while(playing==true)
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
synthesis.speak(sentence_next);
} catch (BusyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoNetworkException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


enter code here


}
}
}

I'm using [the iSpeech library's语音合成 class](http://www.ispeech.org/androidsdkdoc/com/ispeech/SpeechSynthesis.html). When I call the说话 method in a for`循环,一次传递一个句子,所有句子同时开始。我想这是因为该方法是非阻塞的。它不是开源的,所以我看不到它是如何工作的。我怎样才能让它在前一句话完成之前不开始一个句子?

最佳答案

通常对于这些类型的库,代码不会等待声音停止播放(有充分的理由)。

来自documentation of your library ,看来您应该在阅读下一句之前用 PLAY_SUCCESSFUL 监听 SpeechSynthesisEvent

如果它适合您的需求,javax.speech.synthesis 有一个相当简单的机制来等待演讲完成,请查看:http://java.sun.com/products/java-media/speech/forDevelopers/jsapi-guide/Synthesis.html

关于java - 如何阻止 SpeechSynthesis.speak(String) ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9750455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com