Android中AlwaysOnHotwordDetector的示例

Mar*_*ose 30 android speech-recognition android-5.0-lollipop

有人可以提供一个如何在Android中使用新的AlwaysOnHotwordDetector类的示例吗?

我想构建一个应用程序,当应用程序在后台运行时,可以检测到"下一个","后退"或"暂停"等热门词.

Vik*_*ram 34

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


如果您要构建特权应用程序,请查看AOSP中的这些测试项目:


在努力做到这一点时,我发现了这个:

AlwaysOnHotwordDetector's 构造函数:

/**
 * @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();
}
Run Code Online (Sandbox Code Playgroud)

这里的利益声明是:

mKeyphraseMetadata = mKeyphraseEnrollmentInfo.getKeyphraseMetadata(text, locale);
Run Code Online (Sandbox Code Playgroud)

怎么KeyphraseEnrollmentInfo#getKeyphraseMetadata(String, Locale)办?

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

在这一点上,我的示例项目一直告诉我:Enrollment application doesn't support keyphrases.因此,进一步挖掘 - 支持关键短语,我们需要提供额外的元数据:

// 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";
Run Code Online (Sandbox Code Playgroud)

此外,我们需要获得android.permission.MANAGE_VOICE_KEYPHRASES许可.这是示例项目卡住的地方:

<!-- 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" />
Run Code Online (Sandbox Code Playgroud)

第三方应用程序无法使用支持热门词检测所需的权限.我仍然无法弄清楚为什么包android.service.voice软件包具有公共访问权限.也许我在这里遗漏了一些东西.