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

Display text recognition result in toast

parent 0c5b2547
......@@ -9,6 +9,8 @@ import android.os.Build;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.annotation.ContentView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
......@@ -21,6 +23,7 @@ import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
......@@ -135,12 +138,20 @@ public class MainActivity extends AppCompatActivity {
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
listener listener = new listener(this);
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
sr.setRecognitionListener(listener);
sr.startListening(recognizerIntent);
return true;
}
public void SetListeningResults(String result) // to display listening results
{
Log.d("Debug", "Best confidence result (from main activity): " + result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
private void requestRecordAudioPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String requiredPermission = Manifest.permission.RECORD_AUDIO;
......
......@@ -9,6 +9,17 @@ import java.util.ArrayList;
class listener implements RecognitionListener
{
boolean result_available;
ArrayList<String> result_strings; // ordered by likeliness
MainActivity mainActivity;
public listener(MainActivity mainActivity)
{
result_available = false;
result_strings = null;
this.mainActivity = mainActivity;
}
public void onReadyForSpeech(Bundle params)
{
Log.d("Debug", "onReadyForSpeech");
......@@ -39,8 +50,6 @@ class listener implements RecognitionListener
Log.d("Debug", "no speech detected " + results);
return;
}
String str = new String();
Log.d("Debug", "onResults " + results);
/* useless because apparently max confidence index is always 0*/
/*ArrayList ConfidenceScores = results.getStringArrayList(SpeechRecognizer.CONFIDENCE_SCORES);
......@@ -54,8 +63,11 @@ class listener implements RecognitionListener
IndexOfMax = i;
}
}*/
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.d("Debug", "Best confidence result: " + (String)data.get(0));
result_strings = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.d("Debug", "Best confidence result: " + (String)result_strings.get(0));
result_available = true;
mainActivity.SetListeningResults((String)result_strings.get(0));
/*
for (int i = 0; i < data.size(); i++)
{
......
Supports Markdown
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