2014-11-09 3 views
28

Qualcuno può fornire un esempio di come utilizzare la nuova classe AlwaysOnHotwordDetector in Android?esempio di AlwaysOnHotwordDetector in Android

Mi piacerebbe creare un'app, che quando l'app è in esecuzione in background, può rilevare una hotword come "next" o "back" o "pause".

risposta

29

A meno che non abbia un enorme punto cieco, non penso che le applicazioni di terze parti possano utilizzare questa API. È strano che a AlwaysOnHotwordDetector (e alle classi correlate VoiceInteractionService ecc.) Sia stato concesso l'accesso pubblico.


Se si sta costruendo un'applicazione privilegiata, guardare attraverso questi progetti di test da AOSP:


Durante il tentativo di fare questo lavoro, mi sono imbattuto in questo modo:

AlwaysOnHotwordDetector's costruttore:

/** 
* @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(); 
} 

La dichiarazione di interesse qui è:

mKeyphraseMetadata = mKeyphraseEnrollmentInfo.getKeyphraseMetadata(text, locale); 

Cosa KeyphraseEnrollmentInfo#getKeyphraseMetadata(String, Locale) fare?

/** 
* 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; 
} 

A questo punto, il mio progetto di esempio continuava a dirmi: Enrollment application doesn't support keyphrases. Così, scavando un po 'più - per sostenere frasi chiave, abbiamo bisogno di fornire ulteriori meta-dati:

// 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"; 

Inoltre, avremo bisogno del permesso di android.permission.MANAGE_VOICE_KEYPHRASES. Qui è dove il progetto di esempio si blocca:

<!-- 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" /> 

L'autorizzazione richiesta per supportare il rilevamento di hotword non è disponibile per le applicazioni di terze parti. Non riesco ancora a capire perché il pacchetto android.service.voice abbia accesso pubblico. Forse mi manca qualcosa qui.

+0

Hai provato a eseguirlo? Sarebbe bello se potessimo accedere a questa API – gregm

+1

@gregm Sì, ci ho provato. Avevo bisogno dell'autorizzazione di 'MANAGE_VOICE_KEYPHRASES', che solo le app di sistema possono richiedere. Sono d'accordo, sarebbe fantastico se potessimo usare questa API. – Vikram

+0

ci sono procedure su come installare un'app come app di sistema, è stata testata? –