watson-developer-cloud / android-sdk

:high_brightness: Android SDK to use the IBM Watson services.
http://watson-developer-cloud.github.io/android-sdk/
Apache License 2.0
146 stars 94 forks source link

[speech-to-text] FATAL EXCEPTION - "MicrophoneCaptureThread.java:63" from example project #17

Closed hendrawd closed 7 years ago

hendrawd commented 7 years ago

[speech-to-text] from example project will throw

12-15 11:11:43.814 1552-2087/com.ibm.watson.developer_cloud.android.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-2
                                                                                                    Process: com.ibm.watson.developer_cloud.android.myapplication, PID: 1552
                                                                                                    java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
                                                                                                        at android.media.AudioRecord.startRecording(AudioRecord.java:986)
                                                                                                        at com.ibm.watson.developer_cloud.android.library.audio.MicrophoneCaptureThread.run(MicrophoneCaptureThread.java:63)

Steps to reproduce

Just use the example project, provide username and password, run

Expected behavior

Not force closed and can accept voice input

Actual behavior

Force closed and throw exception

Android Phone

One Plus One(A001)

CyanogenMod version 14.1-20161123-NIGHTLY-bacon

Android version 7.1

android-sdk version

Same with example project:

compileSdkVersion 23

buildToolsVersion "23.0.2"

minSdkVersion 9

targetSdkVersion 23

germanattanasio commented 7 years ago

@hendrawd After looking at #18, seems like today is not a good day to try this SDK 😛.

The problem seems to be related to the permissions, do you have

<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>

?

hendrawd commented 7 years ago

@germanattanasio as i mentioned in the first post, i clone it from example project and run it. The example project itself has <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> as in https://github.com/watson-developer-cloud/android-sdk/blob/master/example/src/main/AndroidManifest.xml#L7

germanattanasio commented 7 years ago

Sorry @hendrawd, I'm not that familiar with the example project.

hendrawd commented 7 years ago

For the next update, i think we can support runtime permission for record audio, so we can target newer android sdk

germanattanasio commented 7 years ago

Just to let you know, we are going to move this repo to be part of the java-sdk next week

troyibm commented 7 years ago

@germanattanasio

Just to let you know, we are going to move this repo to be part of the java-sdk next week

Was this repo moved? I'm not sure what that means or how that fixes the problem in this issue. I still get this problem.

hendrawd commented 7 years ago

@troyibm you can implement runtime permission by yourself to record audio or change targetSdkVersion to 22

arprasto commented 7 years ago

i am also facing same issue with below: android { compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { minSdkVersion 21 targetSdkVersion 25}

with below dependency: compile 'com.ibm.watson.developer_cloud:android-sdk:0.2.3' compile 'com.ibm.watson.developer_cloud:java-sdk:3.8.0'

but when i changed targetSdkVersion to 22 it did solved the issue.

germanattanasio commented 7 years ago

@lpatino10 can you look into this too?

hendrawd commented 7 years ago

Actually it's the problem with runtime permission. Please use SDK version 22 if you don't support runtime permission on the example project yet

arprasto commented 7 years ago

Hi @hendrawd I am doing below in my android code:


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case REQUEST_RECORD_AUDIO_PERMISSION:
                permissionToRecordAccepted  = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                break;
            case RECORD_REQUEST_CODE: {

                if (grantResults.length == 0
                        || grantResults[0] !=
                        PackageManager.PERMISSION_GRANTED) {

                    Log.i(TAG, "Permission has been denied by user");
                } else {
                    Log.i(TAG, "Permission has been granted by user");
                }
                return;
            }
            case CameraHelper.REQUEST_PERMISSION:
                permissionOfCamera = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                break;
            case CameraHelper.REQUEST_IMAGE_CAPTURE:
                permissionOfCamera = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                break;
        }
        if (!permissionToRecordAccepted || !permissionOfCamera) finish();
    }
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(FullscreenActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
                return;
            }
``
I am actually trying to support runtime permission. Am I doing anything wrong here ??
lpatino10 commented 7 years ago

@hendrawd what exactly is the issue when running your code? When does it crash and what is the error message?

hendrawd commented 7 years ago

@lpatino10 actually as you can see, i included my stack trace at the first comment. It crash when i tried to get the voice input, maybe when click the record button. @arprasto i don't know, you should follow the runtime permission from google documentation I haven't tried the example project again after my last update and i can't check it right now, so sorry if i can't help. I will check again if i have time.

lpatino10 commented 7 years ago

Hey thanks for the patience. I just made a pull request that I think should fix this issue. Be sure to follow the example code when using the microphone and feel free to open another issue if it's in fact not resolved.