音声認識を使用するには、android.speechパッケージ - RecognizerIntentクラスを使用します。
Android1.5から、音声認識フレームワークとして、
音声認識ライブラリをインテント経由で使用するためのRecognizerIntent(インテント)が追加されました。
簡単に言うと、「音声認識したいよ(※1)」というアクションのインテントを発行すると、
以下のような音声認識画面(アクティビティ)が起動します。
※1:具体的には、ACTION_RECOGNIZE_SPEECH、または、ACTION_WEB_SEARCH

画面が表示されたら、マイクに向かって話しかけると、音声認識が実行され、
ACTION_RECOGNIZE_SPEECHを発行していた場合、認識された音声を文字列(リスト)として取得することができ、
ACTION_WEB_SEARCHを発行していた場合、認識された音声を使用してウェブ検索した結果が、画面表示されます。
実際にソースコードを見てもらった方が早いと思いますので、以下に記載しておきます。
サンプルソースコード
package com.adakoda.voicerecognitiontest;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.adakoda.voicerecognitiontest.R.id;
public class VoiceRecognitionTestActivity extends Activity {
// = 0 の部分は、適当な値に変更してください(とりあえず試すには問題ないですが)
private static final int REQUEST_CODE = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
// インテント作成
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH); // ACTION_WEB_SEARCH
intent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(
RecognizerIntent.EXTRA_PROMPT,
"VoiceRecognitionTest"); // お好きな文字に変更できます
// インテント発行
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// このインテントに応答できるアクティビティがインストールされていない場合
Toast.makeText(VoiceRecognitionTestActivity.this,
"ActivityNotFoundException", Toast.LENGTH_LONG).show();
}
}
});
}
// アクティビティ終了時に呼び出される
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 自分が投げたインテントであれば応答する
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String resultsString = "";
// 結果文字列リスト
ArrayList<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
for (int i = 0; i< results.size(); i++) {
// ここでは、文字列が複数あった場合に結合しています
resultsString += results.get(i);
}
// トーストを使って結果を表示
Toast.makeText(this, resultsString, Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
サンプルリソース
※RecognizerIntentを使用するために必要なリソースでなく、あくまでも今回のサンプルを動作させるために必要なリソース
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button"
android:text="Click to start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
実行結果
実行後、ボタンを押すと音声認識画面(アクティビティ)が起動するので、(マイクに向かって)適当に話しかけてください。
無事認識されると、結果がトーストで表示されます。
ただし、このインテントに応答できるアクティビティがインストールされていない場合(例えば、エミュレーター環境などの場合)、
ActivityNotFoundExceptionが発生しますので、try-catchの例外処理は実装しておきましょう。
※実機は、ADP1で確認しました

その他
ソースコードを書かずに、手っ取り早く試すには、
「APIDemos」サンプルアプリ-「App」-「Voice Recognition」を実行させてみてください。