Troy
Troy

Reputation: 188

Android app crashes when starting to record voice using Watson STT java-sdk

I've had this code working fine on my device before but now it doesn't work at all. Device is Samsung SM-G900P (Android 6.0.2, API23).

I've had to build the speech-to-text, core, and text-to-speech libraries myself since other parts of my app require newer version of OkHttp library. I've built from PR #557.

Here is the error I get when running through Android Studio.

E/AudioRecord: AudioFlinger could not create record track, status: -1
E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.

E/AndroidRuntime: FATAL EXCEPTION: Thread-5952
              java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
                  at android.media.AudioRecord.startRecording(AudioRecord.java:943)
                  at com.ibm.watson.developer_cloud.android.library.audio.MicrophoneCaptureThread.run(MicrophoneCaptureThread.java:63)

Some code...

private void startListening() {

    if (listening != true) {
        capture = new MicrophoneInputStream(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    speechService.recognizeUsingWebSocket(capture, getRecognizeOptions(), new MicrophoneRecognizeDelegate());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        listening = true;
    } else {
        try {
            capture.close();
            listening = false;
            //recordInstance.release();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

private class MicrophoneRecognizeDelegate implements RecognizeCallback {

    @Override
    public void onTranscription(SpeechResults speechResults) {
        System.out.println(speechResults);
        String text = speechResults.getResults().get(0).getAlternatives().get(0).getTranscript();
        showMicText(text);
        if (speechResults.getResults().get(0).isFinal()) {
            try {
                capture.close();
                listening = false;
                send(speechResults.getResults().get(0).getAlternatives().get(0).getTranscript());
            } catch (Exception e) {
                System.out.println("error when closing capture");
                e.printStackTrace();
            }
        }

    }

    @Override public void onConnected() {
        System.out.println("mic connected");
    }

    @Override public void onError(Exception e) {
        e.printStackTrace();
    }

    @Override public void onDisconnected() {
        System.out.println("mic disconnected");
    }
}

Thanks

Upvotes: 1

Views: 704

Answers (1)

Hsaylor
Hsaylor

Reputation: 256

Looks to me like you don't have the proper permissions to record audio; typically that's what AudioFlinger could not create record track, status: -1 indicates. Starting with API 23 you need to request certain permissions at app runtime; audio recording falls within that set. Check out https://developer.android.com/training/permissions/requesting.html

Upvotes: 3

Related Questions