gpt4 book ai didi

安卓 "speak failed: not bound to tts engine"

转载 作者:IT老高 更新时间:2023-10-28 23:13:11 26 4
gpt4 key购买 nike

我的 android 应用程序具有文本朗读功能,并且不止一个 Activity 使用此功能。因此创建了一个静态帮助类来简化此操作。

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 触发 Activity ,我会收到此错误。否则,如果从应用程序代码手动打开 Activity 没有问题。

这是广播接收器代码

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

最佳答案

您的代码没有等待 TextToSpeech 类的构造函数的异步返回。你甚至有一条评论说它是异步的。为什么它总是在接收器中失败,但不是 Activity 我不确定 - 除了可能作为前台任务的 Activity 具有更高的优先级并且它恰好在您调用 sayText 之前完成。

如果它必须新建一个新的 TTS 对象,您需要等待调用 sayText 直到 init 调用返回。

关于安卓 "speak failed: not bound to tts engine",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11778606/

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