gpt4 book ai didi

java - 无需按钮即可在 Activity 开始时播放文本到语音

转载 作者:行者123 更新时间:2023-12-02 00:17:13 25 4
gpt4 key购买 nike

我在我的应用程序中使用文本转语音,并且我已经能够让它在我的所有按钮上正常工作。但是,当我尝试在启动 Activity 中使用文本转语音时,应用程序崩溃了。这是一个空指针异常,所以我知道我只是错误地编码了它。为了澄清我想要它做什么。我希望它在泼水 Activity 期间说话。当启动 Activity hibernate 时,我希望它再次说话,告诉用户它已完成加载。我已经为启动 Activity 包含了 java。

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);
Thread logoTimer = new Thread() {
public void run() {
try {
try {
sleep(5000);
speakWords("loading");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Intent menuIntent = new Intent("android.intent.action.MENU");
startActivity(menuIntent);

Intent checkTTSIntent = new Intent();
checkTTSIntent
.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
}

finally {
finish();
}
}

};
logoTimer.start();
}

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

// speak straight away
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();
}
}

}

最佳答案

在你 sleep 后,你就在线程的开头调用speakWords。调用 myTTS.speak。此时查看您的代码,myTTS 似乎尚未初始化并且为 null,因此会因 NPE 而崩溃。

这段代码应该可以防止 NPE,但是如果 TTS 引擎的初始化时间太长,那么您将不会看到“正在加载”。另外,我猜测 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();
}

Intent menuIntent = new Intent("android.intent.action.MENU");
startActivity(menuIntent);

}

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();
}
}

}

关于java - 无需按钮即可在 Activity 开始时播放文本到语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11688274/

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