【问题标题】:"speak failed: not bound to TTS engine" inside Service服务中的“说话失败:未绑定到 TTS 引擎”
【发布时间】:2015-11-29 23:40:18
【问题描述】:

我有一项服务,当用户进入某个地理区域时我必须发言(由地理围栏监控)。以下是我的代码:

public class GeoIntentService extends Service implements TextToSpeech.OnInitListener {
private TextToSpeech engine;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate(){
    engine = new TextToSpeech(this,
            this  // OnInitListener
    );
    engine.setLanguage(Locale.ENGLISH);
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (!geofencingEvent.hasError()) {
        sendNotification(this, getTriggeringGeofences(intent));
    return Service.START_NOT_STICKY;
}
    return flags;
}

@Override
public void onDestroy() {
    while  (engine.isSpeaking()) {
        System.out.println("Hey engine is still speaking...");
    }
    if(engine!=null){
        engine.stop();
        engine.shutdown();
    }
    super.onDestroy();
}

@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        int result = engine.setLanguage(Locale.US);
        isInit = true;
        if (result == TextToSpeech.LANG_MISSING_DATA ||
                result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.v("OnInit", "Language is not available.");
        } else {
            Log.v("OnInit","Language Set");
        }
    } else {
        Log.v("onInit", "Could not initialize TextToSpeech.");
    }

}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void sendNotification(Context context, String notificationText) {
    System.out.println("Time speak start: " + new Date(System.currentTimeMillis()));
    engine.speak("You are approaching Bus Stop " + notificationText,
            TextToSpeech.QUEUE_FLUSH, null, null);
    while (engine.isSpeaking()) {
        System.out.println("Hey engine is still speaking inside...");
    }
    stopSelf();
    engine.shutdown();
    System.out.println("Time speak end: " + new Date(System.currentTimeMillis()));
    //engine.shutdown();
}

private String getTriggeringGeofences(Intent intent) {
    GeofencingEvent geofenceEvent = GeofencingEvent.fromIntent(intent);
    List<Geofence> geofences = geofenceEvent
            .getTriggeringGeofences();

    String[] geofenceIds = new String[geofences.size()];

    for (int i = 0; i < geofences.size(); i++) {
        geofenceIds[i] = geofences.get(i).getRequestId();
    }

    return TextUtils.join(", ", geofenceIds);
}

}

我收到错误:Speak failed not bound to TTS engine。这里有什么问题?我正在将引擎绑定到 TextToSpeech。我像这样绑定这个服务:

private PendingIntent createRequestPendingIntent() {
        if (mPendingIntent == null) {
            Log.v(TAG, "Creating PendingIntent");
            Intent intent = new Intent(mContext, GeoIntentService.class);
            mPendingIntent = PendingIntent.getService(mContext, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
        }

        return mPendingIntent;
    }

【问题讨论】:

  • 你在哪里得到这个错误?在代码的哪一行?

标签: android google-maps android-intent android-activity android-service


【解决方案1】:

您遇到了时间问题。

当您的Service 通过PendingIntent 启动时,Android 将创建Service 的实例,然后调用onCreate(),然后调用onStartCommand()。您正在尝试使用 onStartCommand() 中的 TTS 引擎讲话。但是 TTS 引擎还没有来得及初始化。

为了让引擎在使用前有足够的时间进行初始化,您需要修改架构。

使用private boolean 变量来跟踪初始化是否完成。如果一切顺利,请将其设置为onInit() 中的true

onStartCommand() 中,通过检查您的private boolean 来检查TTS 初始化是否已完成。如果有,您可以立即发言。如果没有,您需要启动一个后台 Thread 循环、等待、检查标志等,直到初始化完成。只有这样,你才能真正使用 TTS 引擎说话。

【讨论】:

    猜你喜欢
    • 2014-02-25
    • 2016-05-18
    • 2012-07-31
    • 1970-01-01
    • 2023-03-06
    • 2021-01-24
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多