Skip to content

Instantly share code, notes, and snippets.

@sakurabird
Created October 24, 2011 13:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakurabird/1309003 to your computer and use it in GitHub Desktop.
Save sakurabird/1309003 to your computer and use it in GitHub Desktop.
Untouchable 音声認識
//インスタンス変数の定義
SpeechRecognizer rec;
//onCreate()で変数にリスナーをセットする
// 音声認識
rec = SpeechRecognizer.createSpeechRecognizer( getApplicationContext() );
rec.setRecognitionListener( new speechListenerAdp() );
//音声認識をスタートする
// SpeechRecognizer
rec.startListening( RecognizerIntent.getVoiceDetailsIntent( getApplicationContext() ) );
//リスナークラスの定義(汚くてすみません)
/***
* SpeechRecognizerのリスナークラス
*
*/
privateclass speechListenerAdp implements RecognitionListener {
@Override
publicvoid onResults( Bundle results ) {
ArrayList< String > strList = results.getStringArrayList( SpeechRecognizer.RESULTS_RECOGNITION ); // 音声認識結果を取得
int result = SpeechAnalyzer.speechToSecond( strList ); // 音声認識結果から秒数値を取得する、この値をタイマーにセットする
if ( result != 0 ) {
tv_sensor_message.setText( R.string.message_timer_start );
// タイマーサービススタート
Intent intent = new Intent( mContext, TimerService.class );
intent.putExtra( "counter", result );
startService( intent );
// 画面メッセージ
tv_sensor_message.setText( R.string.message_cancel );
} else {
// 取得した文字列が数値じゃなければエラーメッセージ
tv_sensor_message.setText( R.string.message_error_analyze );
tv.setText( showTime( 0 ) );
// 音を出す
sensorcatch.start();
sensorcatch.setOnCompletionListener( new OnCompletionListener() {
publicvoid onCompletion( MediaPlayer mp ) {
// TODO Auto-generated method stub
// 音声認識スタート
tv_sensor_message.setText( R.string.message_talk_to_phone );
startSpeechRecognizer();
}
} );
}
}
@Override
publicvoid onBeginningOfSpeech() {
// 画面メッセージ
tv_sensor_message.setText( R.string.message_wait_recognize );
}
// 発声の終了
@Override
publicvoid onEndOfSpeech() {
// 実は、onResultで、音声認識の結果が返ってくるよりも先に、onEndOfSpeechが発生する
// 画面メッセージ
tv_sensor_message.setText( R.string.message_wait_analyze );
}
@Override
publicvoid onError( int error ) {
// 画面メッセージ
tv_sensor_message.setText( R.string.message_error_recognize );
tv.setText( showTime( 0 ) );
switch ( error ) {
case SpeechRecognizer.ERROR_NO_MATCH: // このエラーが出ると、音声認識が停止してしまうので、再度startListening
// 音を出す
sensorcatch.start();
sensorcatch.setOnCompletionListener( new OnCompletionListener() {
publicvoid onCompletion( MediaPlayer mp ) {
rec.startListening( RecognizerIntent
.getVoiceDetailsIntent( getApplicationContext() ) );
}
} );
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: // 長時間放置で発生、音声認識が停止するので再度startListening
rec.startListening( RecognizerIntent
.getVoiceDetailsIntent( getApplicationContext() ) );
break;
case SpeechRecognizer.ERROR_NETWORK: // インターネットにつながっていない場合なので、Toastでメッセージを出し、アプリを終了
Toast.makeText( UntouchableTimerActivity.this,
getResources().getText( R.string.message_error_network ),
Toast.LENGTH_SHORT ).show();
finish();
break;
case SpeechRecognizer.ERROR_CLIENT: // 何らかのエラーが出た場合、このエラーも発生、対処不要
case SpeechRecognizer.ERROR_SERVER: // このエラーが、どういうケースで発生するか、未確認、ときどき発生する、対処は不要
case SpeechRecognizer.ERROR_AUDIO: // どういうケースで発生するか未確認、対処不要
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: // おそらく音声認識が許可されていない場合発生、対処不要
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: // おそらく音声認識サービスがビジーの場合発生、対処不要
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: // おそらくネットワークからレスポンスが帰ってこない場合発生、対処不要
default: // 発生しないはず
break;
}
}
@Override
publicvoid onReadyForSpeech( Bundle params ) {
// 画面メッセージ
tv_sensor_message.setText( R.string.message_talk_to_phone );
}
@Override
publicvoid onBufferReceived( byte[] arg0 ) {
}
@Override
publicvoid onEvent( int arg0, Bundle arg1 ) {
}
@Override
publicvoid onPartialResults( Bundle arg0 ) { // 認識途中の部分的な結果が必要な場合に使うが、基本的に実装不要
}
@Override
publicvoid onRmsChanged( float arg0 ) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment