|
|||||
音声認識(SpeechRecognizer)Androidでは音声認識を行うとき、RecognizerIntentの他にSpeechRecognizerも利用することができる。RecognizerIntentでは音声認識の際にマイクのプロンプトが表示されてしまうのに対し、SpeechRecognizerの場合にはそのようなプロンプトは表示されない。 音声認識(RecognizerIntent)SpeechRecognizerを利用するにはまず、createSpeechRecognizerメソッドでSpeechRecognizerのインスタンスを生成する。次にsetRecognitionListenerメソッドで認識結果を受け取るためのリスナをセットする。このリスナはRecognitionListenerをimplementsしたクラスのインスタンスである必要がある。その後、startListeningメソッドで音声認識を開始する。 尚、SpeechRecognizerを使用するためには、RECORD_AUDIOアクセス権が必要となるのでAndroidManifest.xmlで予め付与しておく。また、SpeechRecognizerクラスのメソッドは、メインスレッドから呼ばなければならないので、その点にも注意が必要である。
package biz.office_matsunaga.android;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SpeechRecognizerTest2Activity extends Activity {
Button button1;
TextView textView1;
SpeechRecognizer sr;
class SpeechListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
}
@Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
}
@Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
@Override
public void onError(int error) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "エラー " + error, Toast.LENGTH_SHORT).show();
}
@Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
@Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
}
@Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "認識開始", Toast.LENGTH_SHORT).show();
}
@Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
String recognizedList = "";
ArrayList<String> candidates = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for(String recognized : candidates) {
Log.d("onResults", recognized);
recognizedList += recognized + "\n";
}
textView1.setText(recognizedList);
}
@Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
textView1 = (TextView)findViewById(R.id.textView1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
sr.setRecognitionListener(new SpeechListener());
licationContext()));
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
sr.startListening(intent);
}
});
}
}
音声認識の結果は、setRecognitionListenerメソッドでセットしたリスナのonResultsイベントで受け取ることができる。onResultsイベントのresults引数に対して、SpeechRecognizer.RESULTS_RECOGNITIONを渡してgetStringArrayListメソッドを呼び出せば、結果をArrayListに抽出することができる。 また、同じリスナのonErrorイベントには、引数としてエラーコードが渡されるので、RecognizerIntentではできない音声認識時のエラーハンドリング等も行うことができる。 (2012/02/19)
Copyright(C) 2004-2013 モバイル開発系(K) All rights reserved.
[Home]
|
|||||