2016-04-07 8 views
13

Sto costruendo un'app che utilizza i comandi vocali per eseguire determinate funzioni. Ho avuto alcuni codici che lavorano da hereInizia il riconoscimento vocale attraverso la voce con una frase come "Ok Google"?

private static final int SPEECH_REQUEST_CODE = 0; 

// Create an intent that can start the Speech Recognizer activity 
private void displaySpeechRecognizer() { 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
// Start the activity, the intent will be populated with the speech text 
    startActivityForResult(intent, SPEECH_REQUEST_CODE); 
} 

// This callback is invoked when the Speech Recognizer returns. 
// This is where you process the intent and extract the speech text from the intent. 
@Override 
protected void onActivityResult(int requestCode, int resultCode, 
     Intent data) { 
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) { 
     List<String> results = data.getStringArrayListExtra(
       RecognizerIntent.EXTRA_RESULTS); 
     String spokenText = results.get(0); 
     // Do something with spokenText 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

Tuttavia, questo approccio deve essere attivato tramite un clic del pulsante. C'è un modo per avviare il riconoscimento vocale attraverso un comando vocale? Come Google Now, dove puoi semplicemente dire "Ok Google", allora aprirà l'attività Riconoscitore vocale e ascolterà i comandi?

Grazie.

risposta

2

Sarà necessario scrivere un servizio per il riconoscimento vocale continuo. E in base agli input che ottieni come riconoscimento vocale la tua frase trigger e agisci.

Questa operazione potrebbe richiedere molto tempo e sarà necessario ottimizzare attivando e arrestando i servizi in orari e schermate appropriati.

La risposta accettata allo this question fornisce un mezzo per ottenere una soluzione simile.