gpt4 book ai didi

java - 简单的 TextToSpeech 代码

转载 作者:行者123 更新时间:2023-11-30 02:15:42 24 4
gpt4 key购买 nike

我想制作一个简单的 TextToSpeech 代码。我的代码在这里:

TextToSpeech tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
tts.speak("This is a Alert Application", TextToSpeech.QUEUE_ADD,null,null);

但是我收到了这个错误:

Error:(100, 28) error: no suitable constructor found for TextToSpeech(MainActivity,MainActivity)
constructor TextToSpeech.TextToSpeech(Context,OnInitListener,String) is not applicable
(actual and formal argument lists differ in length)
constructor TextToSpeech.TextToSpeech(Context,OnInitListener) is not applicable
(actual argument MainActivity cannot be converted to OnInitListener by method invocation conversion)

我错过了什么?我需要在代码中输入什么?

最佳答案

如果您允许您的线程 hibernate ,并且在 hibernate 之后调用 tts.speak()。如果是这样,此时查看您的代码,tts 似乎没有初始化并且为空,因此会因异常而崩溃。

这段代码应该可以防止异常,但是如果TTS引擎的初始化时间太长,那么你就不会说Loading了。另外,我猜 5 秒(顺便说一句,这是一段很长的时间) sleep 是为了让它得到初始化?

代码

   public class mainj extends Activity implements OnInitListener {

private TextToSpeech myTTS;
// status check code
private int MY_DATA_CHECK_CODE = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadscreen);
Intent checkTTSIntent = new Intent();
checkTTSIntent
.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
Thread logoTimer = new Thread() {
public void run() {
try {
try {
sleep(5000);
speakWords("loading");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//waiting for the sleep
splash_wait()

}

finally {
finish();
}
}

};
logoTimer.start();
}

// speak the user text
private void speakWords(String speech) {

// speak straight away
if(myTTS != null)
{
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
}

// act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// the user has the necessary data - create the TTS
myTTS = new TextToSpeech(this, this);
} else {
// no data - install it now
Intent installTTSIntent = new Intent();
installTTSIntent
.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}

// setup TTS
public void onInit(int initStatus) {

// check for successful instantiation
if (initStatus == TextToSpeech.SUCCESS) {
if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.US);
} else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...",
Toast.LENGTH_LONG).show();
}

private void splash_wait() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

Intent i = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}

}

关于java - 简单的 TextToSpeech 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389488/

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