gpt4 book ai didi

java - setOnUtteranceProgressListener 根本不适用于 API > 21 的文本转语音

转载 作者:太空狗 更新时间:2023-10-29 16:26:13 25 4
gpt4 key购买 nike

我希望 setOnUtteranceProgressListener 应该在语音完成后通知一个 Toast。它似乎不起作用。我使用了 setOnUtteranceProgressListener 并且在 speak 函数中我提到了 paramaters 如下..

    Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, MainActivity.this.getPackageName());

我在调用 speak 函数时给出了一个“UniqueId”,如下所示。

    myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,params,"UniqueId");

在我的程序中,文本到语音引擎完成朗读后,它应该运行一个 Toast 通知它已经完成朗读。但是 setOnUtteranceProgressListner 似乎不起作用。

    myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {

}

@Override
public void onDone(String utteranceId) {

Toast.makeText(MainActivity.this,"Finished speaking.",Toast.LENGTH_LONG).show();
}

@Override
public void onError(String utteranceId) {

}
});

全部代码如下..

    public class MainActivity extends AppCompatActivity {
String message;
private TextToSpeech myTTS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(myTTS.getEngines().size() == 0){
Toast.makeText(MainActivity.this,"No Engines Installed",Toast.LENGTH_LONG).show();
}else{
myTTS.setLanguage(Locale.US);

if (status == TextToSpeech.SUCCESS){
//Toast.makeText(MainActivity.this,"Status working.",Toast.LENGTH_LONG).show();
message = "How may i help you.";
}

}
}
});

myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {

}

@Override
public void onDone(String utteranceId) {

Toast.makeText(MainActivity.this,"onDone working.",Toast.LENGTH_LONG).show();
}

@Override
public void onError(String utteranceId) {

}
});
}

请给出解决方案。

最佳答案

主要问题是:

1) 在tts初始化前设置进度监听。

2) 尝试从后台线程制作 Toast。

我还有一些其他建议的更改,但它们不是必需的:

public class MainActivity extends AppCompatActivity {
String message = "How may I help you?";
String mostRecentUtteranceID;
private TextToSpeech myTTS;

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

myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(myTTS.getEngines().size() == 0){
Toast.makeText(MainActivity.this,"No Engines Installed",Toast.LENGTH_LONG).show();
}else{
if (status == TextToSpeech.SUCCESS){
ttsInitialized();
}
}
}
});
}

private void ttsInitialized() {

// *** set UtteranceProgressListener AFTER tts is initialized ***
myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {

}

@Override
// this method will always called from a background thread.
public void onDone(String utteranceId) {

// only respond to the most recent utterance
if (!utteranceId.equals(mostRecentUtteranceID)) {
Log.i("XXX", "onDone() blocked: utterance ID mismatch.");
return;
} // else continue...

boolean wasCalledFromBackgroundThread = (Thread.currentThread().getId() != 1);
Log.i("XXX", "was onDone() called on a background thread? : " + wasCalledFromBackgroundThread);

Log.i("XXX", "onDone working.");

// for demonstration only... avoid references to
// MainActivity (unless you use a WeakReference)
// inside the onDone() method, as it
// can cause a memory leak.
runOnUiThread(new Runnable() {
@Override
public void run() {
// *** toast will not work if called from a background thread ***
Toast.makeText(MainActivity.this,"onDone working.",Toast.LENGTH_LONG).show();
}
});
}

@Override
public void onError(String utteranceId) {

}
});

// set Language
myTTS.setLanguage(Locale.US);

// set unique utterance ID for each utterance
mostRecentUtteranceID = (new Random().nextInt() % 9999999) + ""; // "" is String force

// set params
// *** this method will work for more devices: API 19+ ***
HashMap<String, String> params = new HashMap<>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, mostRecentUtteranceID);

myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,params);

}

}

关于java - setOnUtteranceProgressListener 根本不适用于 API > 21 的文本转语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52233235/

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