我有一个服务,当用户进入一个地理区域(由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;
}
你在哪里得到这个错误?在哪行代码? –