在使用朗读之前如何确保文本到语音转换已初始化?



我想请你帮忙TextToSpeechAndroid 功能。 基本上,我想开发一个简单的AI,它会说话,提出问题然后等待答案,最后,基于答案提出另一个问题,依此类推,直到用户发音一个停止一切的关键字。

现在我知道在使用speak方法之前必须初始化TextToSpeech,我正在尝试使用onActivityResult方法考虑到这一点。

下面是一些代码:

活动类:

public class MainActivity extends AppCompatActivity implements OnInitListener, Button.OnClickListener{
Button sayHello;
TextView textView;
private static final int CHECK_DATA = 0;
private static final Locale defaultLocale = Locale.UK;   // British English
private static final String TAG = "TTS";
private TextToSpeech tts;
private boolean isInit = false;

说它的方法:用来说话:

public void sayIt(String text, boolean flushQ){
if(isInit){
if(flushQ){
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
} else {
tts.speak(text, TextToSpeech.QUEUE_ADD, null, null);
}
} else {
Log.i(TAG, "Failure: TTS instance not properly initialized");
}
}

文本转语音侦听器:

@Override
public void onInit(int status){
if(status == TextToSpeech.SUCCESS){
isInit = true;
// Enable input text field and speak button now that we are initialized
sayHello.setEnabled(true);
// Set to a language locale after checking availability
Log.i(TAG, "available="+tts.isLanguageAvailable(Locale.UK));
tts.setLanguage(defaultLocale);
// Examples of voice controls.  Set to defaults of 1.0.
tts.setPitch(1.0F);
tts.setSpeechRate(1.0F);
// Issue a greeting and instructions in the default language
tts.speak("Initialized!", TextToSpeech.QUEUE_FLUSH, null, Integer.toString(12));
} else {
isInit = false;
Log.i(TAG, "Failure: TTS instance not properly initialized");
}
}

按钮侦听器:

@Override
public void onClick(View v){
if(isInit)
sayIt("You clicked!", true);
}

活动结果方法:

// Create the TTS instance if TextToSpeech language data are installed on device.  If not
// installed, attempt to install it on the device.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CHECK_DATA) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// Success, so create the TTS instance.  But can't use it to speak until
// the onInit(status) callback defined below runs, indicating initialization.
Log.i(TAG, "Success, let's talk");
tts = new TextToSpeech(this,  this);
// Use static Locales method to list available locales on device
Locale[] locales = Locale.getAvailableLocales();
Log.i(TAG,"Locales Available on Device:");
for(int i=0; i<locales.length; i++){
String temp = "Locale "+i+": "+locales[i]+" Language="
+locales[i].getDisplayLanguage();
if(locales[i].getDisplayCountry() != "") {
temp += " Country="+locales[i].getDisplayCountry();
}
Log.i(TAG, temp);
}
} else {
// missing data, so install it on the device
Log.i(TAG, "Missing Data; Install it");
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}

最后,onCreate方法:

@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.activity_main);
sayHello = findViewById(R.id.sayBtn);
textView = findViewById(R.id.textView);
sayHello.setEnabled(false);
sayHello.setOnClickListener(this);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, CHECK_DATA);
/* THIS SPEAK DOES NOT WORK! */
sayIt("Speech from method!", true);
}

问题是:当该方法初始化TextToSpeech并成功发音文本时onInitButton成功启用。 我的目标是从onCreate方法中Activityspeak,因为目前它仅适用于onInitonClick的侦听器,机器人不在onCreate,即使我使用onActivityResult检查tts初始化。

基本上,我希望TextToSpeech说话时不涉及Buttons

我知道已经发布了非常相似的问题,但没有一个能解决我的问题。有想法吗? 希望我已经清楚了,谢谢!

更新:日志显示检测到错误发生在onInit方法else分支中,其中Log.i(TAG, "Failure: TTS instance not properly initialized");行。

解决方案:这里唯一要做的是等待一点时间,以便TextToSpeech永久初始化。 一个好方法似乎是使用延迟Handler,如下所示:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Waiting for RobotTextToSpeech initialization for 1500ms
rtts.speak("This speak will work!");
rtts.speak("This other speak will work too!");
}
}, 1500);
}

通过这样做,看起来即使在onCreate方法中TextToSpeech也能很好地工作,我们只需要等待很短的时间。 希望这能有所帮助。

相关内容

最新更新