SpeechRecognizer クラスを使用して Android で音声認識を行い、RecognitionListener に影響を与えようとしています。
問題 : 影響を受けるリスナーが呼び出されません。
私の主な活動のコードは次のとおりです。
package com.example.testvoicepaper;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
public int VOICE_RECOGNITION_REQUEST_CODE = 1;
private Button launchRecognition;
private TextView textRecognised;
private ImageView picture;
private SpeechRecognizer sr;
private Context context;
private MyRecognitionListener listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textRecognised = (TextView) findViewById(R.id.textRecognised);
launchRecognition = (Button) findViewById(R.id.launchRecognition);
picture = (ImageView) findViewById(R.id.picture);
context = this;
sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
listener = new MyRecognitionListener();
sr.setRecognitionListener(listener);
launchRecognition.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
sr.startListening(RecognizerIntent.getVoiceDetailsIntent(context));
listener.onBeginningOfSpeech();
break;
}
case MotionEvent.ACTION_UP: {
listener.onEndOfSpeech();
sr.stopListening();
break;
}
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class MyRecognitionListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
Log.d("Speech", "onBeginningOfSpeech");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.d("Speech", "onBufferReceived");
}
@Override
public void onEndOfSpeech() {
Log.d("Speech", "onEndOfSpeech");
}
@Override
public void onError(int error) {
Log.d("Speech", "onError" + error);
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.d("Speech", "onEvent");
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.d("Speech", "onPartialResults");
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.d("Speech", "onReadyForSpeech");
}
@Override
public void onResults(Bundle results) {
Log.d("Speech", "onResults");
ArrayList strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < strlist.size();i++ ) {
Log.d("Speech", "result=" + strlist.get(i));
}
}
@Override
public void onRmsChanged(float rmsdB) {
Log.d("Speech", "onRmsChanged");
}
}
}
これは beginOfSpeech() と endOfSpeech() を呼び出しますが、リスナー関数は呼び出しません。