我正在使用Android上的库皮科声作为服务。
public class PicovoiceService extends Service {
private final String ACCESS_KEY = "MyAccessKey"; // AccessKey obtained from Picovoice Console (https://picovoice.ai/console/)
private static final String CHANNEL_ID = "PicovoiceServiceChannel";
private PicovoiceManager picovoiceManager;
private final PicovoiceWakeWordCallback picovoiceWakeWordCallback = () -> {
Notification n = getNotification("Picovoice", "Wake Word Detected...");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.notify(1234, n);
};
private final PicovoiceInferenceCallback picovoiceInferenceCallback = inference -> {
StringBuilder builder = new StringBuilder();
if (inference.getIsUnderstood()) {
builder.append(inference.getIntent()).append(" - ");
final Map<String, String> slots = inference.getSlots();
if (slots.size() > 0) {
for (String key : slots.keySet()) {
builder.append(key).append(" : ").append(slots.get(key)).append(" ");
}
}
} else {
builder.append("Didn't understand the command.");
}
Notification n = getNotification("Picovoice", builder.toString());
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.notify(1234, n);
};
private final PicovoiceManagerErrorCallback picovoiceManagerErrorCallback = error -> {
onPicovoiceError(error.getMessage());
};
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
CHANNEL_ID,
"Picovoice",
NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(notificationChannel);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
createNotificationChannel();
String keywordFilePath = intent.getStringExtra("keywordFileName");
String contextPath = intent.getStringExtra("contextFileName");
boolean started = false;
try {
picovoiceManager = new PicovoiceManager.Builder()
.setAccessKey(ACCESS_KEY)
.setPorcupineModelPath("porcupine_params_de.pv")
.setRhinoModelPath("porcupine_params_de.pv")
.setKeywordPath(keywordFilePath)
.setPorcupineSensitivity(0.7f)
.setWakeWordCallback(picovoiceWakeWordCallback)
.setContextPath(contextPath)
.setRhinoSensitivity(0.25f)
.setInferenceCallback(picovoiceInferenceCallback)
.setProcessErrorCallback(picovoiceManagerErrorCallback)
.build(getApplicationContext());
picovoiceManager.start();
started = true;
} catch (PicovoiceInvalidArgumentException e) {
onPicovoiceError(
String.format(
"%s\nEnsure your AccessKey '%s' is a valid access key.",
e.getLocalizedMessage(),
ACCESS_KEY));
} catch (PicovoiceActivationException e) {
onPicovoiceError("AccessKey activation error");
} catch (PicovoiceActivationLimitException e) {
onPicovoiceError("AccessKey reached its device limit");
} catch (PicovoiceActivationRefusedException e) {
onPicovoiceError("AccessKey refused");
} catch (PicovoiceActivationThrottledException e) {
onPicovoiceError("AccessKey has been throttled");
} catch (PicovoiceException e) {
onPicovoiceError("Failed to initialize Picovoice " + e.getMessage());
}
Notification notification = started ?
getNotification("Picovoice", "Listening...") :
getNotification("Picovoice init failed", "Service will shutdown");
startForeground(1234, notification);
return super.onStartCommand(intent, flags, startId);
}
private void onPicovoiceError(String message) {
Intent i = new Intent("PicovoiceError");
i.putExtra("errorMessage", message);
sendBroadcast(i);
}
private Notification getNotification(String title, String message) {
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
new Intent(this, MainActivity.class),
0);
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
try {
picovoiceManager.stop();
} catch (PicovoiceException e) {
Log.e("Picovoice", e.toString());
}
super.onDestroy();
}
}当该服务启动时,我将收到通知。
Picovoice init失败 服务将关闭
这是因为started被设置为false。在调试时,我从来没有到过started设置为true的地步。为什么?
我的AccessKey是正确的,但是在尝试调试时,我得到了异常。
ai.picovoice.picovoice.PicovoiceInvalidArgumentException:初始化失败。
请注意,我刚刚编辑了一些关于该代码的细节,因为大部分代码来自作者本人。参见原码 (演示)。
发布于 2021-12-26 22:57:53
我的错误是setRhinoModelPath("porcupine_params_de.pv")。应该用setRhinoModelPath("picovoice_params_de.pv")代替。
https://stackoverflow.com/questions/70476227
复制相似问题