gpt4 book ai didi

java - 在 Text to Speech 中不总是调用 UtteranceProgressListener?

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:45 26 4
gpt4 key购买 nike

我的代码:

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.UtteranceProgressListener;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
@SuppressLint("NewApi")
public class MainActivity extends Activity {
public TextToSpeech tts;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tts = new TextToSpeech(this, new ttsInitListener());
}
@SuppressLint("TrulyRandom") class ttsInitListener implements OnInitListener {

@SuppressWarnings("deprecation")
@Override
public void onInit(int status) {

if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.getDefault());
if (!tts.isSpeaking()) {
SecureRandom random = new SecureRandom();
String aa= new BigInteger(130, random).toString(32);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, aa);
tts.speak("Hello Hello HEllo HEllo HEllo",
TextToSpeech.QUEUE_FLUSH, map);
tts.setOnUtteranceProgressListener(new ttsUtteranceListener());
}

} else {
tts = null;
Toast.makeText(getApplicationContext(),
"Failed to initialize TTS engine.", Toast.LENGTH_SHORT)
.show();
}
}
}

class ttsUtteranceListener extends UtteranceProgressListener {

@Override
public void onDone(String utteranceId) {

Toast.makeText(getApplicationContext(), "onDone", Toast.LENGTH_LONG).show();

}

@Override
public void onError(String utteranceId) {
Toast.makeText(getApplicationContext(), "onError", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(String utteranceId) {
Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_LONG).show();
}
}
}

什么时候说话

tts.speak("Hello Hello HEllo HEllo HEllo",
TextToSpeech.QUEUE_FLUSH, map);

完成之后应该调用 ttsUtteranceListener 类的 onDone(String utteranceId) 方法。但是 ttsUtteranceListener 并不总能到达。

为什么这个类没有达到?

最佳答案

你正在使用这个:

String aa= new BigInteger(130, random).toString(32);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, aa); //aa can be anything random, not necessary a word contained in your text

UtteranceProgressListener 是与话语通过合成队列的进度相关的事件的监听器。

onDone : Called when an utterance has successfully completed processing.

你应该尝试使用这个:

HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "Hello");
tts.speak("Hello Hello HEllo HEllo HEllo", TextToSpeech.QUEUE_FLUSH, map);
tts.setOnUtteranceProgressListener(new ttsUtteranceListener());

然后当 Hello 的发音成功完成时它会被触发。

希望对您有所帮助。

关于java - 在 Text to Speech 中不总是调用 UtteranceProgressListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27520018/

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