Ses girişi

Her Wear OS cihazda mikrofon bulunur. Böylece kullanıcılar, seslerini kullanarak cihazla etkileşim kurabilir. Bunları üç tür etkileşime ayırabilirsiniz:

  • Ses kaydetme
  • Serbest biçimli konuşma girişi alma
  • Sesli İşlemler

Ses kaydetme

Wear OS cihazlarında ses kaydı, telefondakiyle aynı şekilde yapılır. Android'de ses kaydetme hakkında daha fazla bilgi edinmek için MediaRecorder belgelerine bakın. Ayrıca, GitHub'daki Wear Speaker örneğinde örnek bir uygulamaya göz atabilirsiniz.

Serbest biçimli konuşma girişi alma

Kullanıcılardan konuşma girişi almak için sistemin yerleşik Konuşma Tanıyıcı etkinliğini çağırın. Mesaj göndermek veya arama yapmak için konuşma girişini kullanın.

Uygulamanızda, ACTION_RECOGNIZE_SPEECH işlemini kullanarak startActivityForResult() numaralı telefonu arayın. Bu işlem, konuşma tanıma etkinliğini başlatır ve daha sonra sonucu onActivityResult() ile işleyebilirsiniz.

Aşağıdaki kod örneğinde, konuşma tanıma etkinliğinin nasıl başlatılacağı ve işleneceği gösterilmektedir.

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

Sesli İşlem

Sesli İşlemler ve Asistan Uygulama İşlemleri şu anda Çin'deki Wear OS uygulamaları hariç desteklenmemektedir. Çin için Sesli İşlem desteği hakkında daha fazla bilgi edinin.