الإدخال الصوتي

إنّ كل جهاز من أجهزة Wear OS مزوَّد بميكروفون لكي يتمكن المستخدمون من استخدام أصواتهم للتفاعل مع الجهاز. يمكنك تقسيم هذه التفاعلات إلى ثلاثة أنواع من التفاعلات:

  • تسجيل الصوت
  • الحصول على ميزة إدخال الكلام الحرة
  • الإجراءات الصوتية

تسجيل الصوت

إنّ ميزة تسجيل الصوت على جهاز Wear OS تعمل بالطريقة نفسها التي تتّبعها على الهاتف. يُرجى مراجعة مستندات MediaRecorder لمعرفة المزيد من المعلومات حول تسجيل الصوت على Android. يمكنك أيضًا إلقاء نظرة على نموذج تنفيذ في نموذج Wear Speaker على GitHub.

الحصول على ميزة إدخال الكلام الحرة

يمكنك استدعاء نشاط أداة التعرّف على الكلام المُدمَج في النظام للحصول على إدخالات الكلام من المستخدمين. يمكنك استخدام إدخال الكلام لإرسال الرسائل أو إجراء عمليات بحث.

في تطبيقك، اتصل بالرقم startActivityForResult() باستخدام الإجراء ACTION_RECOGNIZE_SPEECH. سيؤدي هذا الإجراء إلى بدء نشاط "التعرّف على الكلام" ويمكنك بعد ذلك معالجة النتيجة في onActivityResult().

يوضح الرمز النموذجي التالي كيفية بدء نشاط التعرّف على الكلام والتعامل معه.

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

الإجراءات الصوتية

إنّ الإجراءات الصوتية وإجراءات التطبيقات في "مساعد Google" غير متاحة في الوقت الحالي، باستثناء تطبيقات Wear OS في الصين. يمكنك الاطّلاع على مزيد من المعلومات حول إتاحة الإجراءات الصوتية في الصين.