例如AlwaysOnHotwordDetector的安卓AlwaysOnHotwordDetector

2023-09-04 08:29:41 作者:青春染指流年

有人可以提供如何使用新的AlwaysOnHotwordDetector类机器人的例子吗​​?

Can someone provide an example of how to use the new AlwaysOnHotwordDetector class in Android?

我想建立一个应用程序,当应用程序在后台运行,可以检测出像下一个,或回,或暂停一启动指令。

I'd like to build an app, that when the app is running in the background, can detect a hotword like "next", or "back", or "pause".

推荐答案

除非我有一个巨大的盲点,我不认为第三方应用程序可以利用该API。它的奇怪的是, AlwaysOnHotwordDetector (和相关类VoiceInteractionService等)已被授予公共访问。

Unless I have a huge blind spot, I don't think third-party applications can make use of this API. Its strange that AlwaysOnHotwordDetector (and related classes VoiceInteractionService etc.) have been granted public access.

如果你正在建设一个享有特权的应用程序,期待通过从AOSP这些测试项目:

If you are building a privileged app, look through these test projects from AOSP:

语音交互 - 基本 AlwaysOnHotwordDetector 用法/执行 http://androidxref.com/5.0.0_r2/xref/frameworks/base/tests/VoiceInteraction/ 在语音报名 - http://androidxref.com/5.0.0_r2/xref/frameworks/base/tests/VoiceEnrollment/ Voice Interaction - Basic AlwaysOnHotwordDetector usage/implementation http://androidxref.com/5.0.0_r2/xref/frameworks/base/tests/VoiceInteraction/ Voice Enrollment - http://androidxref.com/5.0.0_r2/xref/frameworks/base/tests/VoiceEnrollment/

在试图使这项工作,我来到这样的:

While trying to make this work, I came upon this:

AlwaysOnHotwordDetector的构造器:

/**
 * @param text The keyphrase text to get the detector for.
 * @param locale The java locale for the detector.
 * @param callback A non-null Callback for receiving the recognition events.
 * @param voiceInteractionService The current voice interaction service.
 * @param modelManagementService A service that allows management of sound models.
 *
 * @hide
 */
public AlwaysOnHotwordDetector(String text, Locale locale, Callback callback,
        KeyphraseEnrollmentInfo keyphraseEnrollmentInfo,
        IVoiceInteractionService voiceInteractionService,
        IVoiceInteractionManagerService modelManagementService) {
    mText = text;
    mLocale = locale;
    mKeyphraseEnrollmentInfo = keyphraseEnrollmentInfo;
    mKeyphraseMetadata = mKeyphraseEnrollmentInfo.getKeyphraseMetadata(text, locale);
    mExternalCallback = callback;
    mHandler = new MyHandler();
    mInternalCallback = new SoundTriggerListener(mHandler);
    mVoiceInteractionService = voiceInteractionService;
    mModelManagementService = modelManagementService;
    new RefreshAvailabiltyTask().execute();
}

在这里感兴趣的语句是:

The statement of interest here is:

mKeyphraseMetadata = mKeyphraseEnrollmentInfo.getKeyphraseMetadata(text, locale);

这是什么 KeyphraseEnrollmentInfo#getKeyphraseMetadata(字符串,区域设置)吗?

/**
 * Gets the {@link KeyphraseMetadata} for the given keyphrase and locale, null if any metadata
 * isn't available for the given combination.
 *
 * @param keyphrase The keyphrase that the user needs to be enrolled to.
 * @param locale The locale for which the enrollment needs to be performed.
 *        This is a Java locale, for example "en_US".
 * @return The metadata, if the enrollment client supports the given keyphrase
 *         and locale, null otherwise.
 */
public KeyphraseMetadata getKeyphraseMetadata(String keyphrase, Locale locale) {
    if (mKeyphrases == null || mKeyphrases.length == 0) {
        Slog.w(TAG, "Enrollment application doesn't support keyphrases");
        return null;
    }
    for (KeyphraseMetadata keyphraseMetadata : mKeyphrases) {
        // Check if the given keyphrase is supported in the locale provided by
        // the enrollment application.
        if (keyphraseMetadata.supportsPhrase(keyphrase)
                && keyphraseMetadata.supportsLocale(locale)) {
            return keyphraseMetadata;
        }
    }
    Slog.w(TAG, "Enrollment application doesn't support the given keyphrase/locale");
    return null;
}

在这一点上,我的示例项目不停地告诉我:注册应用程序不支持的关键字句。因此,挖掘远一点 - 支持关键字句,我们需要提供更多的元数据:

At this point, my example project kept telling me: Enrollment application doesn't support keyphrases. So, digging a bit further - to support keyphrases, we need to provide additional meta-data:

// Taken from class KeyphraseEnrollmentInfo
/**
 * Name under which a Hotword enrollment component publishes information about itself.
 * This meta-data should reference an XML resource containing a
 * <code>&lt;{@link
 * android.R.styleable#VoiceEnrollmentApplication
 * voice-enrollment-application}&gt;</code> tag.
 */
private static final String VOICE_KEYPHRASE_META_DATA = "android.voice_enrollment";

此外,我们将需要 android.permission.MANAGE_VOICE_KEYPHRASES 许可。这是项目的例子卡:

Additionally, we will need the android.permission.MANAGE_VOICE_KEYPHRASES permission. This is where the example project gets stuck:

<!-- Must be required by hotword enrollment application,
     to ensure that only the system can interact with it.
     @hide <p>Not for use by third-party applications.</p> -->
<permission android:name="android.permission.MANAGE_VOICE_KEYPHRASES"
    android:label="@string/permlab_manageVoiceKeyphrases"
    android:description="@string/permdesc_manageVoiceKeyphrases"
    android:protectionLevel="signature|system" />

支持启动指令检测所需的权限是不提供给第三方应用程序。我还是不明白,为什么包 android.service.voice 封装具有公共访问。也许我失去了一些东西。

The permission required to support hotword detection is not available to third-party applications. I still can't figure out why package android.service.voice package has public access. Perhaps I am missing something here.

相关推荐