However, the UI is pre-defined.
Can we implement our own UI for that?
For such purpose, we need to add SpeechRecognizer as show below:
Intent recognizerIntent; private SpeechRecognizer sr; sr = SpeechRecognizer.createSpeechRecognizer(this); sr.setRecognitionListener(new listener()); recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);//5 is the number of results to return //To start to recognize...put the code in onClick() event of some button sr.startListening(recognizerIntent); //To stop to recognize...put the code in onClick() event of some button //In general, the recognition process will stop automatically after completion. //If we want to interrupt it or some abnormal situations happened, we can use the codes below.. sr.stopListening(); sr.cancel();The class listener() used is shown below:
private static final int RECOGNIZER_ERROR_NETWORK_TIMEOUT = 1;//Network timeout
private static final int RECOGNIZER_ERROR_NETWORK = 2;//Other network related errors
private static final int RECOGNIZER_ERROR_AUDIO = 3;//Cannot record the voice
private static final int RECOGNIZER_ERROR_SERVER = 4;//Server is abnormal
private static final int RECOGNIZER_ERROR_CLIENT = 5;//Other abnormal cases in client side
private static final int RECOGNIZER_ERROR_SPEECH_TIMEOUT = 6;//No voice input
private static final int RECOGNIZER_ERROR_NO_MATCH = 7;//No matched result
private static final int RECOGNIZER_ERROR_RECOGNIZER_BUSY = 8;//RecognitionService is busy
private static final int RECOGNIZER_ERROR_INSUFFICIENT_PERMISSIONS = 9;//No sufficient permission
class listener implements RecognitionListener
{
public void onReadyForSpeech(Bundle params)
{
//Ready to accept the voice input. We can show some icon to notify the user that he/she can start talking
}
public void onBeginningOfSpeech()
{
//User start to talk. We can show some icon to tell the user that the program has got the input voice.
}
public void onRmsChanged(float rmsdB)
{
//The intensity of input voice, ranged from 0 to 10.
//We can show some icon according to the intensity.
//onRmsChanged() will be called frequently. We'd better update the UI if its value varies larger than some range.
}
public void onBufferReceived(byte[] buffer)
{
}
public void onEndOfSpeech()
{
//User stop talking. The server will start to recognize.
}
public void onError(int error)
{
switch(error)
{
case RECOGNIZER_ERROR_NETWORK_TIMEOUT:
break;
case RECOGNIZER_ERROR_NETWORK:
break;
case RECOGNIZER_ERROR_AUDIO:
break;
case RECOGNIZER_ERROR_SERVER:
break;
case RECOGNIZER_ERROR_CLIENT:
break;
case RECOGNIZER_ERROR_SPEECH_TIMEOUT:
break;
case RECOGNIZER_ERROR_NO_MATCH:
break;
case RECOGNIZER_ERROR_RECOGNIZER_BUSY:
break;
case RECOGNIZER_ERROR_INSUFFICIENT_PERMISSIONS:
break;
}
}
public void onResults(Bundle results)
{
//The recognition result
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String firstMatched = (String) data.get(0);
txtResult.setText(firstMatched);
}
public void onPartialResults(Bundle partialResults)
{
}
public void onEvent(int eventType, Bundle params)
{
}
}
沒有留言:
張貼留言