最近は「OK Google」で周知されてきた音声認識機能ですが、RecognizerIntent.ACTION_RECOGNIZE_SPEECH を使って音声認識を実装することができます。
簡単に機能を確認する例です
![[Android] 音声認識 RecognizerIntent 1x1.trans - [Android] 音声認識 RecognizerIntent](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif) ボタンタップ
ボタンタップ
![[Android] 音声認識 RecognizerIntent 1x1.trans - [Android] 音声認識 RecognizerIntent](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif) 「テスト」と話しかける
「テスト」と話しかける
![[Android] 音声認識 RecognizerIntent 1x1.trans - [Android] 音声認識 RecognizerIntent](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif) 「テスト」が表示された
「テスト」が表示された
Android Studio
2021.1.1
2021.1.1
RecognizerIntent.ACTION_RECOGNIZE_SPEECH
言語認識は、サーバーに飛ばして結果を受取ります。基本的にはネットに接続していないと使えませんが、言語認識のパッケージが端末にインストールされているのであればオフラインでも可能です。
 
Intent を使って Activity を遷移し、遷移先から結果を受け取るという仕組みです。
Actvity 画面遷移とデータ受け渡しが参考になります
 
サンプルコード
結果は registerForActivityResult() で受け取りますが、いくつもの候補リストで返ってきます。ここでは順位1位のものを表示させています。
 
MainActivity.java
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | //package your.package.name; import android.os.Bundle; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.speech.RecognizerIntent; import android.widget.Button; import android.widget.TextView; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; import java.util.Locale; public class MainActivity extends AppCompatActivity {     private TextView textView;     private int lang ;     ActivityResultLauncher<Intent> resultLauncher = registerForActivityResult(             new ActivityResultContracts.StartActivityForResult(),             result -> {                 if (result.getResultCode() == Activity.RESULT_OK) {                     Intent resultData = result.getData();                     if (resultData != null) {                         // 認識結果を ArrayList で取得                         ArrayList candidates =                                 resultData.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);                         if(candidates.size() > 0) {                             // 認識結果候補で一番有力なものを表示                             textView.setText( candidates.get(0));                         }                     }                 }             });     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         // 言語選択 0:日本語、1:英語、2:オフライン、その他:General         lang = 0;         // 認識結果を表示させる         textView = findViewById(R.id.text_view);         Button buttonStart = findViewById(R.id.button_start);         buttonStart.setOnClickListener(v -> {             // 音声認識を開始             speech();         });     }     private void speech(){         // 音声認識の Intent インスタンス         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);         if(lang == 0){             // 日本語             intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.JAPAN.toString() );         }         else if(lang == 1){             // 英語             intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH.toString() );         }         else if(lang == 2){             // Off line mode             intent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);         }         else{             intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,                     RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);         }         intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 100);         intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "音声を入力");         try {             // インテント発行             resultLauncher.launch(intent);         }         catch (ActivityNotFoundException e) {             e.printStackTrace();             textView.setText(R.string.error);         }     } } | 
 
レイアウトです
activity_main.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:gravity="center"     tools:context=".MainActivity">     <TextView         android:id="@+id/text_view"         android:text="@string/text"         android:textSize="30sp"         android:layout_width="wrap_content"         android:layout_height="wrap_content" />     <Button         android:id="@+id/button_start"         android:text="@string/button"         android:layout_margin="30dp"         android:textSize="30sp"         android:layout_width="wrap_content"         android:layout_height="wrap_content" /> </LinearLayout> | 
 
リソース
strings.xml
| 1 2 3 4 5 6 | <resources>     <string name="app_name">YourAppName</string>     <string name="error">No Activity</string>     <string name="text">Test RecognizerIntent</string>     <string name="button">Speech</string> </resources> | 
 
これは最低限の機能検証ですので実際のアプリに組み込むにはこの他にも色々手当をする必要はあります。
