0

I have been exploring the voice recognition sector of Android Studio. This was my first time developing an application with TextToSpeech and SpeechRecognizer. Whenever I tap on the voice button the app crashes. Please be aware that I am a beginner who does not have a thorough grasp of the Java.

    package com.blindnavi.navirecog;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.Date;
import java.util.List;
import java.util.Locale;

public class NaviCaroline extends AppCompatActivity {

private TextToSpeech NaviVoice;
private SpeechRecognizer NaviSay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navi_caroline);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
            NaviSay.startListening(intent);
        }
    });

    initializeTextToSpeech();
    initializeSpeechRecognizer();
}

private void initializeSpeechRecognizer() {
    if(SpeechRecognizer.isRecognitionAvailable(this)) {
        NaviSay = SpeechRecognizer.createSpeechRecognizer(this);
        NaviSay.setRecognitionListener((new RecognitionListener() {
            @Override
            public void onReadyForSpeech(Bundle params) {

            }

            @Override
            public void onBeginningOfSpeech() {

            }

            @Override
            public void onRmsChanged(float rmsdB) {

            }

            @Override
            public void onBufferReceived(byte[] buffer) {

            }

            @Override
            public void onEndOfSpeech() {

            }

            @Override
            public void onError(int error) {

            }

            @Override
            public void onResults(Bundle bundle) {
                List<String> results = bundle.getStringArrayList(
                        SpeechRecognizer.RESULTS_RECOGNITION
                );
                processResult(results.get(0));
            }

            @Override
            public void onPartialResults(Bundle partialResults) {

            }

            @Override
            public void onEvent(int eventType, Bundle params) {

            }
        }));
    }
}

private void processResult(String command) {
    command = command.toLowerCase();

    //what is your name
    //what is the time
    //open a browser

    if(command.indexOf("what") != -1) {
        if (command.indexOf("your name") != -1) {
            speak("My name is Caroline.");
        }
        if (command.indexOf("time") != -1) {
            Date now = new Date();
            String time = DateUtils.formatDateTime(this, now.getTime(),
                    DateUtils.FORMAT_SHOW_TIME);
            speak("The time now is " + time);
        }
    } else if(command.indexOf("open") != -1) {
        if(command.indexOf("browser") != -1) {
            Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https:/tutsplus.com"));
            startActivity(intent);
        }
    }
}

private void initializeTextToSpeech() {
    NaviVoice = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(NaviVoice.getEngines().size() == 0) {
                Toast.makeText(NaviCaroline.this, "There is no TextToSpeech engine on your device.",
                        Toast.LENGTH_LONG);
                finish();
            } else {
                NaviVoice.setLanguage(Locale.UK);
                speak("Welcome to BlindNavi.");
            }
        }
    });
}

private void speak(String message) {
    if(Build.VERSION.SDK_INT >= 21) {
        NaviVoice.speak(message, TextToSpeech.QUEUE_FLUSH, null, null);
    } else {
        NaviVoice.speak(message, TextToSpeech.QUEUE_FLUSH, null);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_navi_caroline, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onPause() {
    super.onPause();
    NaviVoice.shutdown();
}
}

Forgive me for adding so much code but I cannot find the issue anywhere.

Your help is greatly appreciated.

P.S The "record_audio" permission was created.

Stack trace:

07-17 16:05:22.062 16743-16743/com.blindnavi.navirecog E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]] 07-17 16:05:22.062 16743-16743/com.blindnavi.navirecog V/BoostFramework: BoostFramework() : mPerf = null 07-17 16:05:22.120 16743-16743/com.blindnavi.navirecog D/ViewRootImpl@e5200a4[NaviCaroline]: ViewPostImeInputStage processPointer 1 07-17 16:05:22.137 16743-16743/com.blindnavi.navirecog D/AndroidRuntime: Shutting down VM 07-17 16:05:22.138 16743-16743/com.blindnavi.navirecog E/AndroidRuntime: FATAL EXCEPTION: main Process: com.blindnavi.navirecog, PID: 16743 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.speech.SpeechRecognizer.startListening(android.content.Intent)' on a null object reference at com.blindnavi.navirecog.NaviCaroline$1.onClick(NaviCaroline.java:45) at android.view.View.performClick(View.java:6205) at android.view.View$PerformClick.run(View.java:23653) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

Kenneth
  • 39
  • 8

1 Answers1

0

You should write your code like this,

if(!SpeechRecognizer.isRecognitionAvailable(this)) {
    Toast.makeText(NaviCaroline.this, "SpeechToText is unavailable.", Toast.LENGTH_LONG);
    finish();
}
else{
    NaviSay = SpeechRecognizer.createSpeechRecognizer(this);
    //initialize stt

I don't know why isRecognitionAvailable return false, but you should check all permissions you requested are granted before call isRecognitionAvailable() on android 6.0 or later version.

Joe Mizuno
  • 437
  • 2
  • 15
  • I added the record_audio permission. Do you know which other permissions would be applicable? – Kenneth Jul 18 '18 at 16:25
  • see this https://stackoverflow.com/questions/42494141/android-speech-recognition-api-does-not-work-in-android-7-nougat/42708108#42708108 and this https://stackoverflow.com/questions/33666071/android-marshmallow-request-permission – Joe Mizuno Jul 18 '18 at 23:51