I will try to use the Speech recognition without Google dialog boxes with RecognitionListener but does not works only beep when start the application.I have added permissions Audio record and Internet into the manifest file.I hope you tell me and help me to find the wrong...I have not errors on Log cat...I want to make a loop when user say hello a Toast show up a mesage Regognition OK and a list view shows the results.
public class MainActivity extends Activity implements RecognitionListener
{
private ListView wordsList;
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
wordsList = (ListView) findViewById(R.id.listView1);
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
public void onBeginningOfSpeech(){ }
public void onBufferReceived(byte[] buffer){ }
public void onEndOfSpeech(){ }
public void onError(int error){
//mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
public void onEvent(int eventType, Bundle params){ }
public void onPartialResults(Bundle partialResults){ }
public void onReadyForSpeech(Bundle params){
Toast.makeText(getBaseContext(), "Voice recording starts", Toast.LENGTH_SHORT).show();
}
public void onResults(Bundle results)
{
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));
if ( matches.contains("hello") {
Toast.makeText(getBaseContext(), "Recognision OK!!!", Toast.LENGTH_SHORT).show();
}
}
public void onRmsChanged(float rmsdB) { }
}
You've missed:
mSpeechRecognizer.setRecognitionListener();
The parameter will be either be the context which implements the listener, or the custom listener you've created.
At the moment your code is not setting the listener, you just have the listener code.
Edit: If you do not implement RecognitionListener
, then you can do it this way:
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
}
@Override
public void onBufferReceived(byte[] arg0) {
// TODO Auto-generated method stub
}
@Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
@Override
public void onError(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onEvent(int arg0, Bundle arg1) {
// 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
}
@Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
}
@Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
});