使用Watson STT java-sdk开始录制语音时Android应用程序崩溃

时间:2017-02-21 21:26:43

标签: android speech-to-text ibm-watson

我之前已经在我的设备上正常运行此代码,但现在它根本不起作用。设备是三星SM-G900P(Android 6.0.2,API23)。

我必须自己构建语音到文本,核心和文本到语音库,因为我的应用程序的其他部分需要更新版本的OkHttp库。我是从PR#557建造的。

以下是我在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)

一些代码......

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");
    }
}

由于

1 个答案:

答案 0 :(得分:3)

在我看来,你不具备录制音频的适当权限;通常是AudioFlinger could not create record track, status: -1表示的内容。从API 23开始,您需要在app运行时请求某些权限;录音属于该组。查看https://developer.android.com/training/permissions/requesting.html