I'm developing an App with SpeechRecognizer
. I will use it in different activities for different uses and It's a bit dirty add the same code all time to different classes. So I moved my custom RecognitionListener
to a new class. In that way I just initialize it when I want from my activities. But I can´t find a way to receive the result of the listener (in this case, an ArrayList
of possible values for the speech recognized) in my current activity to use it.
I have tried to implement it through an interface, but I think that I did it in a wrong way. My Listener code is this:
public class SpeechRecognitionListener implements RecognitionListener
{
private final String TAG = "SpeechRecognitionListener";
private Intent mSpeechRecognizerIntent;
private SpeechRecognizer mSpeechRecognizer;
public SpeechRecognitionListener(Intent speechRecognizerIntent, SpeechRecognizer speechRecognizer ) {
mSpeechRecognizerIntent = speechRecognizerIntent;
mSpeechRecognizer = speechRecognizer;
}
@Override
public void onBeginningOfSpeech()
{
//Log.d(TAG, "onBeginingOfSpeech");
}
@Override
public void onBufferReceived(byte[] buffer)
{
}
@Override
public void onEndOfSpeech()
{
//Log.d(TAG, "onEndOfSpeech");
}
@Override
public void onError(int error)
{
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
//Log.d(TAG, "error = " + error);
}
@Override
public void onEvent(int eventType, Bundle params)
{
}
@Override
public void onPartialResults(Bundle partialResults)
{
}
@Override
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$
}
@Override
public void onResults(Bundle results)
{
//I want to recieve this array in my main activity
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
}
@Override
public void onRmsChanged(float rmsdB)
{
}
}
I just want to receive the onResult()
array in my current activity to work with it.
Try to define an interface first:
public interface RecognitionCallback
{
abstract void onRecoginitionFinished(ArrayList<String> matches);
}
Now let your activity that needs to be called back implement this interface. For example:
public class MainActivity extends AppCompatActivity implements RecognitionCallback {
...
public void onRecognitionFinished(ArrayList<String> matches)
{
//do your things with the data
}
}
Also add some properties of SpeechRecognitionListener class:
public class SpeechRecognitionListener implements RecognitionListener
{
private final String TAG = "SpeechRecognitionListener";
private Intent mSpeechRecognizerIntent;
private SpeechRecognizer mSpeechRecognizer;
private RecognitionCallback mCallback
public SpeechRecognitionListener(Intent speechRecognizerIntent, SpeechRecognizer speechRecognizer, RecognitionCallback callback ) {
mSpeechRecognizerIntent = speechRecognizerIntent;
mSpeechRecognizer = speechRecognizer;
mCallback = callback;
...
public void onResults(Bundle results)
{
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
mCallback.onRecognitionFinished(matches);
}
}
And finally in your activity where you need to get called back write this:
SpeechRecognitionListener listener = new SpeechRecognitionLinstner(intent,recognizer,this);