どの Wear OS デバイスにもマイクが搭載されており、ユーザーはデバイスを声で操作できます。これは次の 3 種類の操作に分けることができます。
- 音声を記録する
- 自由形式の音声入力を取得する
- 音声操作
音声を記録する
Wear OS デバイスでの音声の記録は、スマートフォンの場合と同様です。Android で音声を記録する方法の詳細については、MediaRecorder のドキュメントをご覧ください。GitHub にある Wear スピーカーのサンプルの実装例もご覧ください。
自由形式の音声入力を取得する
システムに組み込まれている音声認識装置のアクティビティを呼び出して、ユーザーからの音声入力を取得します。音声入力を使用して、メッセージの送信や検索を行います。
アプリで、ACTION_RECOGNIZE_SPEECH
アクションを使用して startActivityForResult()
を呼び出します。これにより、音声認識アクティビティが開始され、その結果を 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); }
音声操作
音声操作とアシスタントの App Actions は、中国の Wear OS アプリを除き、現時点ではサポートされていません。詳しくは、中国での音声操作のサポートに関するページをご覧ください。