1

I'm trying to detect speech for a feature in a machine learning algorithm, but I'm having a hard time running the Speech Recognition API in the background and while the phone is off. I've tried to put the SpeechRecognizer in a service, however, the service gets killed and reports this error:

E/ActivityThread: Service com.test.speechrecognition.services.SpeechDetectionService has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@914d0fc that was originally bound here android.app.ServiceConnectionLeaked: Service com.test.speechrecognition.services.SpeechDetectionService has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@914d0fc that was originally bound here at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:1835) at android.app.LoadedApk.getServiceDispatcherCommon(LoadedApk.java:1707) at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1686) at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1819) at android.app.ContextImpl.bindService(ContextImpl.java:1749) at android.content.ContextWrapper.bindService(ContextWrapper.java:756) at android.speech.SpeechRecognizer.startListening(SpeechRecognizer.java:286) at com.test.speechrecognition.services.SpeechDetectionService$1.run(SpeechDetectionService.java:56) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7664) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I'm needing the application to track if it detected speech every minute ever ten minutes or so.

Here's the MainActivity.java and SpeechDetectionService.java code:

Main Activity:

public class MainActivity extends AppCompatActivity {
// Create speechRecognizer object
private SpeechRecognizer speechRecognizer;
public static final Integer RecordAudioRequestCode = 1;

private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Check if permissions have been granted
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
        checkPermission();
    }
}

@Override
protected void onStart() {
    super.onStart();
    Log.d(TAG, "onStart:start SpeechDetectionService");
    startService(new Intent(this, SpeechDetectionService.class));
}

// Permission check function
private void checkPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO},RecordAudioRequestCode);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == RecordAudioRequestCode && grantResults.length > 0) {
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
        }
    }
}

SpeechDetectionService:

public class SpeechDetectionService extends Service {
private static final String TAG = SpeechDetectionService.class.getSimpleName();

private SpeechRecognizer speechRecognizer;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    Log.d(TAG, "onBind()");
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate()");


}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    Log.d(TAG, "onStartCommand");


    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);

    final Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());



    final Handler handler = new Handler();
    final int delay = 10000;
    handler.postDelayed(new Runnable() {
        public void run() {
            speechRecognizer.startListening(speechRecognizerIntent);
            handler.postDelayed(this, delay);

        }
    }, delay);

    speechRecognizer.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 results) {

        }

        @Override
        public void onPartialResults(Bundle partialResults) {

        }

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

        }
    });
    return START_STICKY;
}
  • 1
    Do you mean when the phone is off (shut down) or an application is closed? – David Lee Jul 07 '21 at 23:18
  • When the screen is off and when the user closes the application, not when the phone is shutdown. – Aaron Walker Jul 08 '21 at 19:53
  • You can use a background service; take a look at [this](https://developer.android.com/guide/components/services) and [this](https://developer.android.com/training/run-background-service/create-service). – David Lee Jul 08 '21 at 22:17
  • I have the speech recognition as a service, and it will run in the background, however, the speech recognition API will stop itself as soon as the app is closed out of. Is there something I'm missing with the API that prevents it from running the background? – Aaron Walker Jul 08 '21 at 22:48

0 Answers0