Commit 0c5b2547 authored by Martin LOISEAU's avatar Martin LOISEAU 🎄
Browse files

Add speech recognition

parent b85c6f7a
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
......
......@@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.qrquest" >
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
......
package com.example.qrquest;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
......@@ -11,18 +13,23 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Environment;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
TextToSpeech tts;
SpeechRecognizer sr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -36,6 +43,7 @@ public class MainActivity extends AppCompatActivity {
}
});
//setRecognitionListener(android.speech.RecognitionListener)
FloatingActionButton photo = findViewById(R.id.photo);
// Should open camera to take a picture of a QR code
......@@ -65,6 +73,7 @@ public class MainActivity extends AppCompatActivity {
public void onClick(View view) {
Snackbar.make(view, "Should listen to what's being said using speech recognition API", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
DemoListener();
}
});
......@@ -73,7 +82,6 @@ public class MainActivity extends AppCompatActivity {
mp3_reader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Should play sound from an mp3 file", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
PlayTestMP3();
......@@ -119,4 +127,29 @@ public class MainActivity extends AppCompatActivity {
return true;
}
public boolean DemoListener()
{
requestRecordAudioPermission();
Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
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,5);
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
sr.startListening(recognizerIntent);
return true;
}
private void requestRecordAudioPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String requiredPermission = Manifest.permission.RECORD_AUDIO;
// If the user previously denied this permission then show a message explaining why
// this permission is needed
if (checkCallingOrSelfPermission(requiredPermission) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{requiredPermission}, 101);
}
}
}
}
package com.example.qrquest;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.SpeechRecognizer;
import android.util.Log;
import java.util.ArrayList;
class listener implements RecognitionListener
{
public void onReadyForSpeech(Bundle params)
{
Log.d("Debug", "onReadyForSpeech");
}
public void onBeginningOfSpeech()
{
Log.d("Debug", "onBeginningOfSpeech");
}
public void onRmsChanged(float rmsdB)
{
Log.d("Debug", "onRmsChanged");
}
public void onBufferReceived(byte[] buffer)
{
Log.d("Debug", "onBufferReceived");
}
public void onEndOfSpeech()
{
Log.d("Debug", "onEndofSpeech");
}
public void onError(int error)
{
Log.d("Debug", "error " + error);
}
public void onResults(Bundle results)
{
if (results == null) {
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);
float MaxConfidence = 0;
int IndexOfMax = 0;
for(int i = 0; i < ConfidenceScores.size(); i++)
{
if((float)ConfidenceScores.get(i) > MaxConfidence)
{
MaxConfidence = (float)ConfidenceScores.get(i);
IndexOfMax = i;
}
}*/
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.d("Debug", "Best confidence result: " + (String)data.get(0));
/*
for (int i = 0; i < data.size(); i++)
{
Log.d("Debug", "result " + data.get(i));
str += data.get(i);
}*/
//mText.setText("results: "+String.valueOf(data.size()));
}
public void onPartialResults(Bundle partialResults)
{
Log.d("Debug", "onPartialResults");
}
public void onEvent(int eventType, Bundle params)
{
Log.d("Debug", "onEvent " + eventType);
}
}
\ No newline at end of file
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