gpt4 book ai didi

java - 无法使用 "TextToSpeech.speak()"方法

转载 作者:太空宇宙 更新时间:2023-11-04 09:26:57 25 4
gpt4 key购买 nike

我之前用过这个代码。当时正在执行同一行。但在这个新应用程序中,同一行代码并未执行。事实上,它会崩溃。

TextToSpeech TTS ;
String fail = "hey";
TTS.speak(fail ,TextToSpeech.QUEUE_ADD,null,null);

^^ 上面的代码不起作用

TextToSpeech TTS;
String SPEAKDRAW = "OH OHH DRAW";
TTS.speak(SPEAKDRAW, TextToSpeech.QUEUE_ADD,null,null);

^^上面的代码完美执行

这是崩溃的 LogCat。

2019-08-19 22:46:05.997 15712-15712/com.example.anonymous D/AndroidRuntime: Shutting down VM
2019-08-19 22:46:06.002 15712-15712/com.example.anonymous E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.anonymous, PID: 15712
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.speak(java.lang.CharSequence, int, android.os.Bundle, java.lang.String)' on a null object reference
at com.example.anonymous.MainActivity$6.onClick(MainActivity.java:454)
at android.view.View.performClick(View.java:6612)
at android.view.View.performClickInternal(View.java:6589)
at android.view.View.access$3100(View.java:785)
at android.view.View$PerformClick.run(View.java:25925)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
2019-08-19 22:46:06.048 15712-15712/com.example.anonymous I/Process: Sending signal. PID: 15712 SIG: 9

最佳答案

如果不查看崩溃日志,很难确定这里的确切问题是什么。但是,我猜您正在测试该功能的设备中未安装 TTS 引擎。我引用的是developers' documentation ,

Although all Android-powered devices that support the TTS functionality ship with the engine, some devices have limited storage and may lack the language-specific resource files. If a user wants to install those resources, the TTS API enables an application to query the platform for the availability of language files and can initiate their download and installation. So upon creating your activity, a good first step is to check for the presence of the TTS resources with the corresponding intent.

因此,我建议您使用以下 Intent 检查您的设备中是否安装了 TTS 引擎。

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

并通过重写 Activity 中的 onActivityResult 函数来检查设备是否已准备就绪,如下所示。

private TextToSpeech mTts;
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
mTts = new TextToSpeech(this, this);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}

此外,我没有看到任何初始化 TTS 变量的 new 语句。但是,我认为您已经做到了,正如您所说的那样,您之前已经可以正常工作了。

希望有帮助!

关于java - 无法使用 "TextToSpeech.speak()"方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57561206/

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