내 안드로이드 응용 프로그램은 기능을 말하기위한 텍스트를 가지고 있으며, 하나의 활동이이 기능을 사용합니다. 이렇게 쉽게 정적 도우미 클래스를 만들었습니다.Android "말하기 실패 : TTS 엔진에 연결되지 않음"
import java.util.Locale;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
public class TextToSpeechController implements OnInitListener {
private static final String TAG = "TextToSpeechController";
private TextToSpeech myTTS;
private String textToSpeak;
private Context context;
private static TextToSpeechController singleton;
public static TextToSpeechController getInstance(Context ctx) {
if (singleton == null)
singleton = new TextToSpeechController(ctx);
return singleton;
}
private TextToSpeechController(Context ctx) {
context = ctx;
}
public void speak(String text) {
textToSpeak = text;
if (myTTS == null) {
// currently can't change Locale until speech ends
try {
// Initialize text-to-speech. This is an asynchronous operation.
// The OnInitListener (second argument) is called after
// initialization completes.
myTTS = new TextToSpeech(context, this);
} catch (Exception e) {
e.printStackTrace();
}
}
sayText();
}
public void onInit(int initStatus) {
if (initStatus == TextToSpeech.SUCCESS) {
if (myTTS.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.UK);
}
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (initStatus == TextToSpeech.SUCCESS) {
int result = myTTS.setLanguage(Locale.UK);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e(TAG, "TTS missing or not supported (" + result + ")");
// Language data is missing or the language is not supported.
// showError(R.string.tts_lang_not_available);
} else {
// Initialization failed.
Log.e(TAG, "Error occured");
}
}
}
private void sayText() {
// ask TTs to say the text
myTTS.speak(this.textToSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
public void stopTTS() {
if (myTTS != null) {
myTTS.shutdown();
myTTS.stop();
myTTS = null;
}
}
}
저는이 헬퍼 클래스를 이와 같이 사용하고 있습니다.
TextToSpeechController.getInstance(this).speak(readableMessage);
가끔 LogCat에서 다음과 같은 오류가 표시됩니다.
"speak:failed not bound to tts engine"
예외는 발생하지 않지만 TTS에서 읽지는 않습니다. BroadcastReceiver에서 활동을 시작하면이 오류가 발생한다는 것을 알았습니다. 그렇지 않으면 응용 프로그램 코드에서 수동으로 열린 활동이 문제없이 작동하는지 여부. 여기
당신의 코드가 TextToSpeech 클래스의 생성자에서 비동기 반환을 기다리고되지 않는 브로드 캐스트 리시버 코드private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
String readableMessage = intent.getExtras().getString(READABLE_MESSAGE);
Bundle b = new Bundle();
b.putString(EXTRA_MESSAGE, newMessage);
b.putString(READABLE_MESSAGE, readableMessage);
Intent newIntent = new Intent("android.intent.action.MAIN");
newIntent.setClass(context, Speak.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.putExtra("MessageReceived", b);
newIntent.putExtra("CallType", CallType.NOTIFICATION);
context.startActivity(newIntent);
}
};
어떻게하면됩니까? 스레드. 잠깐? 나는 말할 수있는 방법이 없으며 말을 할 때까지 기다린다. –
안녕하세요. 이 TTS 스레드를 병렬로 시작할 수 있습니까? –