音声操作は、ウェアラブルの操作における重要な要素です。音声操作により、ユーザーはハンズフリーですばやく操作を実行できます。Wear OS by Google では、次の 2 種類の音声操作を使用できます。
- システム提供
- この音声操作はタスクベースで、Wear プラットフォームに組み込まれています。音声操作によって開始するアクティビティでは、音声操作にフィルタを適用します。例として、「メモを入力」や「アラームを設定」などがあります。
- アプリ提供
- この音声操作はアプリベースで、ランチャー アイコンと同じように宣言します。ユーザーは「"アプリ名" を開始して」と話しかけて、アプリ提供の音声操作と、開始を指定したアクティビティを使用します。
システム提供の音声操作を宣言する
Wear OS プラットフォームには、「メモを入力」や「アラームを設定」などのユーザー操作に基づく音声インテントがいくつか用意されています。これにより、ユーザーが実行したい操作を話しかけて、開始する最適なアクティビティをシステムに理解させることができます。
ユーザーが音声操作を行ったとき、アクティビティを開始するために起動されたインテントに対してアプリでフィルタを適用することができます。バックグラウンドでなんらかの処理を行うためにサービスを開始する場合、アクティビティをビジュアル キューとして表示し、アクティビティ内でサービスを開始します。ビジュアル キューを取り除く場合は、finish()
を呼び出します。
たとえば、「メモを入力」コマンドでは、次のインテント フィルタを宣言し、MyNoteActivity
という名前のアクティビティを開始します。
<activity android:name="MyNoteActivity"> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="com.google.android.voicesearch.SELF_NOTE" /> </intent-filter> </activity>
以下に、Wear プラットフォームでサポートされている音声インテントを示します。
名前 | フレーズの例 | インテント |
---|---|---|
タクシーを呼ぶ | 「OK Google、タクシーを呼んで」 「OK Google、タクシーを手配して」 |
|
メモを入力 | 「OK Google、メモを取って」 「OK Google、自分用メモ」 |
|
アラームを設定 | 「OK Google、アラームを午前 8 時にセットして」 「OK Google、明日の朝 6 時に起こして」 |
|
タイマーを設定 | 「OK Google、タイマーを 10 分に設定して」 |
|
ストップウォッチを開始 | 「OK Google、ストップウォッチを開始」 |
|
サイクリングを開始 / 終了 | 「OK Google、サイクリングを開始」 「OK Google、自転車に乗る」 「OK Google、サイクリングを終了」 |
|
ランニングを開始 / 終了 | 「OK Google、ランニングを管理して」 「OK Google、ランニングを開始」 「OK Google、ランニングを終了」 |
|
ワークアウトを開始 / 終了 | 「OK Google、ワークアウトを開始」 「OK Google、ワークアウトを管理して」 「OK Google、ワークアウトを停止」 |
|
心拍数を表示 | 「OK Google、心拍数はいくつ?」 「OK Google、bpm はいくつ?」 |
|
歩数を表示 | 「OK Google、歩数を教えて」 「OK Google、歩数はいくつ?」 |
|
プラットフォーム インテントへの登録とインテントに含まれるエクストラ情報へのアクセスに関するドキュメントについては、一般的なインテントをご覧ください。
アプリ提供の音声操作を宣言する
プラットフォームの音声インテントがどれも正常に機能しない場合、「MyActivityName を開始」音声操作によってアプリを直接起動できます。
「開始」アクションに登録する方法は、ハンドヘルドでランチャー アイコンに登録する方法と同じです。ランチャーでアプリアイコンをリクエストする代わりに、アプリで音声操作をリクエストします。
「~を開始」の「~」の部分のテキストを指定するには、開始するアクティビティの label
属性を指定します。たとえば、次のインテント フィルタは、「MyRunningApp を開始」音声操作を認識し、StartRunActivity
を開始します。
<application> <activity android:name="StartRunActivity" android:label="MyRunningApp"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
自由形式の音声入力を取得する
音声操作を使用してアクティビティを開始するだけでなく、システムに組み込まれている音声認識装置のアクティビティを呼び出して、ユーザーからの音声入力を取得することもできます。この機能は、ユーザーからの入力を取得して処理する(たとえば、検索を実行する、メッセージとして送信する)場合に役立ちます。
アプリでは、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) } // Start the activity, the intent will be populated 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); // Start the activity, the intent will be populated 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); }
以下の関連リソースもご覧ください。