有人可以提供一个示例,说明如何在Android中使用新的AlwaysOnHotwordDetector类吗?
我想构建一个应用程序,当该应用程序在后台运行时,它可以检测到诸如“下一个”,“后退”或“暂停”之类的关键词。
最佳答案
除非我有巨大的盲点,否则我认为第三方应用程序无法利用此API。奇怪的是AlwaysOnHotwordDetector
(以及相关类VoiceInteractionService等)已被授予公共(public)访问权限。
如果您要构建特权应用程序,请查看来自AOSP的以下测试项目:
AlwaysOnHotwordDetector
基本用法/实现http://androidxref.com/5.0.0_r2/xref/frameworks/base/tests/VoiceInteraction/
在尝试进行这项工作时,我遇到了这个问题:
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();
}
感兴趣的声明是:
mKeyphraseMetadata = mKeyphraseEnrollmentInfo.getKeyphraseMetadata(text, locale);
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;
}
此时,我的示例项目一直告诉我:
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><{@link
* android.R.styleable#VoiceEnrollmentApplication
* voice-enrollment-application}></code> tag.
*/
private static final String VOICE_KEYPHRASE_META_DATA = "android.voice_enrollment";
此外,我们将需要
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" />
支持热字检测所需的权限不适用于第三方应用程序。我仍然不知道为什么
android.service.voice
包具有公共(public)访问权限。也许我在这里错过了一些东西。关于android - Android中的AlwaysOnHotwordDetector的示例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26834163/