音声機能を追加する

音声操作は、ウェアラブルの操作における重要な要素です。音声操作により、ユーザーはハンズフリーですばやく操作を実行できます。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、タクシーを手配して」
アクション
com.google.android.gms.actions.RESERVE_TAXI_RESERVATION
メモを入力 「OK Google、メモを取って」

「OK Google、自分用メモ」
アクション
android.intent.action.SEND
カテゴリ
com.google.android.voicesearch.SELF_NOTE
エクストラ
android.content.Intent.EXTRA_TEXT - メモの本文を示す文字列
アラームを設定 「OK Google、アラームを午前 8 時にセットして」

「OK Google、明日の朝 6 時に起こして」
アクション
android.intent.action.SET_ALARM
エクストラ
android.provider.AlarmClock.EXTRA_HOUR - アラーム時刻の「時」を示す整数

android.provider.AlarmClock.EXTRA_MINUTES - アラーム時刻の「分」を示す整数

(これら 2 つのエクストラはオプションです。両方とも指定しないか、両方とも指定します)

タイマーを設定 「OK Google、タイマーを 10 分に設定して」
アクション
android.intent.action.SET_TIMER
エクストラ
android.provider.AlarmClock.EXTRA_LENGTH - タイマーの長さを表す 1~86400(24 時間に対応する秒数)の範囲の整数
ストップウォッチを開始 「OK Google、ストップウォッチを開始」
アクション
com.google.android.wearable.action.STOPWATCH
サイクリングを開始 / 終了 「OK Google、サイクリングを開始」

「OK Google、自転車に乗る」

「OK Google、サイクリングを終了」
アクション
vnd.google.fitness.TRACK
MIME タイプ
vnd.google.fitness.activity/biking
エクストラ
actionStatus - 開始時の ActiveActionStatus と終了時の CompletedActionStatus の値を含む文字列
ランニングを開始 / 終了 「OK Google、ランニングを管理して」

「OK Google、ランニングを開始」

「OK Google、ランニングを終了」
アクション
vnd.google.fitness.TRACK
MIME タイプ
vnd.google.fitness.activity/running
エクストラ
actionStatus - 開始時の ActiveActionStatus と終了時の CompletedActionStatus の値を含む文字列
ワークアウトを開始 / 終了 「OK Google、ワークアウトを開始」

「OK Google、ワークアウトを管理して」

「OK Google、ワークアウトを停止」
アクション
vnd.google.fitness.TRACK
MIME タイプ
vnd.google.fitness.activity/other
エクストラ
actionStatus - 開始時の ActiveActionStatus と終了時の CompletedActionStatus の値を含む文字列
心拍数を表示 「OK Google、心拍数はいくつ?」

「OK Google、bpm はいくつ?」
アクション
vnd.google.fitness.VIEW
MIME タイプ
vnd.google.fitness.data_type/com.google.heart_rate.bpm
歩数を表示 「OK Google、歩数を教えて」

「OK Google、歩数はいくつ?」
アクション
vnd.google.fitness.VIEW
MIME タイプ
vnd.google.fitness.data_type/com.google.step_count.cumulative

プラットフォーム インテントへの登録とインテントに含まれるエクストラ情報へのアクセスに関するドキュメントについては、一般的なインテントをご覧ください。

アプリ提供の音声操作を宣言する

プラットフォームの音声インテントがどれも正常に機能しない場合、「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);
    }
    

以下の関連リソースもご覧ください。