Input vocale

Ogni dispositivo Wear OS è dotato di un microfono, così gli utenti possono interagire con il dispositivo usando la voce. Puoi dividerli in tre tipi di interazioni:

  • Registra audio
  • Ricevi input vocale in formato libero
  • Azioni vocali

Registra audio

La registrazione dell'audio su un dispositivo Wear OS funziona esattamente come su uno smartphone. Per scoprire di più sulla registrazione di audio su Android, consulta la documentazione di MediaRecorder. Puoi anche guardare un'implementazione di esempio nell'esempio di Wear Speaker su GitHub.

Ricevi input vocale in formato libero

Richiama l'attività di riconoscimento vocale integrata del sistema per ricevere input vocali dagli utenti. Usa i comandi vocali per inviare messaggi o eseguire ricerche.

Nell'app, chiama startActivityForResult() utilizzando l'azione ACTION_RECOGNIZE_SPEECH. Verrà avviata l'attività di riconoscimento vocale e potrai quindi gestire il risultato in onActivityResult().

Il seguente esempio di codice mostra come avviare e gestire un'attività di riconoscimento vocale.

Kotlin

private const val SPEECH_REQUEST_CODE = 0
...
// Create an intent that can start the Speech Recognizer activity
private fun displaySpeechRecognizer() {
    val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
        putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
    }
    // This starts the activity and populates the intent 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 fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        val spokenText: String? =
                data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).let { results ->
                    results[0]
                }
        // Do something with spokenText.
    }
    super.onActivityResult(requestCode, resultCode, data)
}

Java

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);
// This starts the activity and populates the intent 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);
}

Azioni vocali

Le Azioni vocali e le Azioni app dell'assistente non sono supportate al momento, ad eccezione delle app per Wear OS in Cina. Ulteriori informazioni sul supporto di Azioni vocali per la Cina.