0
我想創建一個使用textToSpeak的應用程序。顯然textToSpeak必須在一個活動中使用才能工作。Android - 需要一個活動,但不想進行一項活動?
但是我不能在正常的java類中使用textToSpeak,所以我可以在當前正在運行的活動中實例化它並調用一個方法,使textToSpeech能夠發出特定的文本。
我設法讓TTS在一個單獨的項目時工作,它是運行的活動,但是我不能讓它在AA不同的一個實例...
說話類(持有TTS,有問題行是「私人文字轉語音TTS =新的文字轉語音(這一點,這一點);」 - 提供了一個未捕獲的異常
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
public class Speech extends Activity implements TextToSpeech.OnInitListener {
private TextToSpeech tts = new TextToSpeech(this, this);
private String toRead;
public void speak()
{
speakOut();
}
public void changeText(String changeTo)
{
toRead = changeTo;
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
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!");
}
}
private void speakOut() {
tts.speak(toRead, TextToSpeech.QUEUE_FLUSH, null);
}
}
運行活動(編輯,以顯示我要如何使用語音類)
public class Text_entry extends Activity implements OnTouchListener{
private Speech speech = new Speech();
public boolean onTouch(View v, MotionEvent event) {
speech.changeText(toRead);
speech.speak();
}
}
這個設置對其他對象來說工作得很好,因爲TTS需要成爲一個活動的一部分(如果語言沒有擴展活動,它是無法識別的)我似乎沒有工作。任何人都可以提供解決方案嗎?
logcat的
02-16 16:52:46.698: D/AndroidRuntime(22541): Shutting down VM
02-16 16:52:46.698: W/dalvikvm(22541): threadid=1: thread exiting with uncaught exception (group=0x415fe300)
02-16 16:52:46.703: E/AndroidRuntime(22541): FATAL EXCEPTION: main
02-16 16:52:46.703: E/AndroidRuntime(22541): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.BT/org.BT.Text_entry}: java.lang.NullPointerException
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.os.Looper.loop(Looper.java:137)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-16 16:52:46.703: E/AndroidRuntime(22541): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 16:52:46.703: E/AndroidRuntime(22541): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 16:52:46.703: E/AndroidRuntime(22541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-16 16:52:46.703: E/AndroidRuntime(22541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-16 16:52:46.703: E/AndroidRuntime(22541): at dalvik.system.NativeStart.main(Native Method)
02-16 16:52:46.703: E/AndroidRuntime(22541): Caused by: java.lang.NullPointerException
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:91)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.speech.tts.TtsEngines.getDefaultEngine(TtsEngines.java:75)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.speech.tts.TextToSpeech.getDefaultEngine(TextToSpeech.java:1235)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:595)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:553)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:527)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:512)
02-16 16:52:46.703: E/AndroidRuntime(22541): at org.BT.Speech.<init>(Speech.java:12)
02-16 16:52:46.703: E/AndroidRuntime(22541): at org.BT.Text_entry.<init>(Text_entry.java:48)
02-16 16:52:46.703: E/AndroidRuntime(22541): at java.lang.Class.newInstanceImpl(Native Method)
02-16 16:52:46.703: E/AndroidRuntime(22541): at java.lang.Class.newInstance(Class.java:1319)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
02-16 16:52:46.703: E/AndroidRuntime(22541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
02-16 16:52:46.703: E/AndroidRuntime(22541): ... 11 more
這絕對已經在朝着正確的方向邁出的一步。我不再有任何錯誤和Logcat狀態我已經成功綁定到com.google.android.tts並連接到TTS服務 現在唯一的問題是沒有聲音:( – user2052839 2013-02-16 17:42:25
@ user2052839手機是否處於USB調試模式?這可能導致沒有聲音出來TTS也可以花一些時間來初始化之前,它能說話 – Eric 2013-02-16 17:43:31
編碼絕對在模擬器上工作...只是要弄清楚爲什麼它不是在電話上。不僅如此,這是行不通的 - 這是TTS在主要活動中不起作用的獨立示例,也是 – user2052839 2013-02-16 17:59:42