Entrada de texto por voz

Todo dispositivo Wear OS vem com um microfone, para que os usuários possam usar a voz para interagir com ele. Você pode dividir as interações em três tipos:

  • Gravar áudio
  • Captar uma entrada de fala livre
  • Ações por voz

Gravar áudio

A gravação de áudio em um dispositivo Wear OS funciona da mesma forma que em um smartphone. Consulte a documentação do MediaRecorder para saber mais sobre como gravar áudio no Android. Confira também uma implementação de exemplo do Wear Speaker (link em inglês) no GitHub.

Captar uma entrada de fala livre

Chame a atividade integrada do reconhecedor de fala do sistema para receber entradas de fala dos usuários. Use a entrada de fala para enviar mensagens ou fazer pesquisas.

No app, chame startActivityForResult() usando a ação ACTION_RECOGNIZE_SPEECH. Isso inicia a atividade de reconhecimento de fala, e você pode processar o resultado em onActivityResult().

O exemplo de código abaixo mostra como iniciar e processar uma atividade de reconhecimento de fala.

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);
}

Ações por voz

No momento, os apps Ações por voz e Ações no app do Google Assistente não têm suporte, exceto em apps para Wear OS na China. Leia mais sobre o suporte a Ações por voz para a China.