Commit 41386670 authored by Martin LOISEAU's avatar Martin LOISEAU 🎄
Browse files

Fix speech recognition

parent 1b6e9f00
......@@ -44,6 +44,7 @@ class Listener implements RecognitionListener
public void onError(int error)
{
Log.d("Debug", "error " + error);
onResults(new Bundle());
}
public void onResults(Bundle results)
{
......
......@@ -318,18 +318,28 @@ public class MainActivity extends AppCompatActivity {
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,2);
}
public void GetSpeech(CaptureSpeech calling_action) {
this.SpeechRequest = calling_action;
listener = new Listener(this);
Log.d("Debug2", "Listener fully initialized");
sr = SpeechRecognizer.createSpeechRecognizer(this);
Log.d("Debug2", "sr = SpeechRecognizer.createSpeechRecognizer(this); called");
sr.setRecognitionListener(listener);
Log.d("Debug2", "Start listening speech recognizer");
sr.startListening(recognizerIntent);
//dirty workaround from: https://stackoverflow.com/questions/5968646/android-speechrecognizer-should-only-be-used-from-the-applications-main-thread
// removing the view trick will cause an error "SpeechRecognizer should be used only from the application's main thread" and not start speech detected
View v = findViewById(R.id.imageView2); //fetch a View: any one will do
final MainActivity m_a = this;
v.post(new Runnable(){ public void run(){
listener = new Listener(m_a);
Log.d("Debug2", "Listener fully initialized");
sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
Log.d("Debug2", "sr = SpeechRecognizer.createSpeechRecognizer(this); called");
sr.setRecognitionListener(listener);
Log.d("Debug2", "Start listening speech recognizer");
sr.startListening(recognizerIntent);
}});
}
public void SetListeningResults(String result) // to display listening results
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment