0

我有一个服务,当用户进入一个地理区域(由Geofence监控)时,我必须说话。以下是我的代码:“发言失败:不绑定到TTS引擎”里面服务

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; 
    } 
+0

你在哪里得到这个错误?在哪行代码? –

回答

0

您有计时问题。

当你Service通过PendingIntent开始,Android将创建Service的一个实例,然后调用onCreate(),然后调用onStartCommand()。您正在尝试使用onStartCommand()中的TTS引擎。但是TTS引擎没有足够的时间来初始化。

为了让引擎在使用之前有足够的时间进行初始化,您需要修改架构。使用private boolean变量来跟踪初始化是否完成。如果一切都成功,请将其设置为true,地址为onInit()

in onStartCommand(),检查您的private boolean是否已完成TTS初始化。如果有,你可以立即说话。如果没有,你需要开始一个背景Thread循环,等一下,检查标志等,直到初始化完成。只有这样,你真的可以使用TTS引擎来说话吗?

相关问题