gpt4 book ai didi

java - 文本转语音产生错误

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

因此,我第一次尝试在我的应用程序中实现文本转语音。当演讲被召集时,我仍然有一个错误的想法。这是我收到的日志:

01-23 18:52:29.825: E/AndroidRuntime(24380): FATAL EXCEPTION: main
01-23 18:52:29.825: E/AndroidRuntime(24380): Process: com.rcd.league, PID: 24380
01-23 18:52:29.825: E/AndroidRuntime(24380): java.lang.NullPointerException
01-23 18:52:29.825: E/AndroidRuntime(24380): at com.rcd.league.speakActivity.speakOut(speakActivity.java:60)
01-23 18:52:29.825: E/AndroidRuntime(24380): at com.rcd.league.spawnTimers$15.onClick(Timers.java:350)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.view.View.performClick(View.java:4442)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.view.View$PerformClick.run(View.java:18423)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.os.Handler.handleCallback(Handler.java:733)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.os.Handler.dispatchMessage(Handler.java:95)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.os.Looper.loop(Looper.java:137)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.app.ActivityThread.main(ActivityThread.java:5083)
01-23 18:52:29.825: E/AndroidRuntime(24380): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 18:52:29.825: E/AndroidRuntime(24380): at java.lang.reflect.Method.invoke(Method.java:515)
01-23 18:52:29.825: E/AndroidRuntime(24380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-23 18:52:29.825: E/AndroidRuntime(24380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-23 18:52:29.825: E/AndroidRuntime(24380): at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

package com.rcd.league;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class speakActivity extends Activity implements TextToSpeech.OnInitListener {

private int MY_DATA_CHECK_CODE = 0;
private TextToSpeech tts;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
tts = new TextToSpeech(this, this);
}

@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}

@Override
public void onInit(int status) {

if (status == TextToSpeech.SUCCESS) {

int result = tts.setLanguage(Locale.US);

if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut();
}

} else {
Log.e("TTS", "Initilization Failed!");
}

}

public void speakOut() {

String text = "test";

tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}



}

我正在调用speakOut();来自另一项 Activity 。这是该代码的剪辑:

speakActivity speak = new speakActivity();

...

    bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (btTimer.getText().toString().contains(":50")){
cd[0].start();
speak.speakOut();

} else {
cd[0].cancel();
cd[0].onFinish();
}

}
});

任何帮助将不胜感激!谢谢!

编辑:我也尝试过 QUEUE_ADD 而不是 QUEUE_FLUSH,但这也不起作用:/

最佳答案

您创建了 speakActivity Activity,但在您发布的代码中您从未启动该 Activity。由于该 Activity 从未启动,因此从未调用 onCreate(),因此 tts 未初始化。这就是为什么在调用时会收到 NullPointerException

tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

speakOut()中。

有关何时调用 onCreate() 的更多信息,请参阅此 stackoverflow 问题:Is onCreate called when an Activity object is created?

关于java - 文本转语音产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21321844/

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