Aditi Goyal
Aditi Goyal

Reputation: 31

Getting E/android.media.AudioRecord: User permission denied! in samsung s8 phone

Getting

E/android.media.AudioRecord: User permission denied!

Error occurs in Samsung S8 running android 7.0.0. However, This error is not seen in moto g3(android version 7.1.1) and one plus 3(android version 8.0.0).I have tried in samsung phones with versions of 7.1.1 (in that also same error appeared.) target sdk version is 25

logs below:

W/IPA-Native-W: data received from IPA is NULL! is it ok?
D/IpaCoreSdkAdapter: ipaMessageEventType= START_RECO
E/android.media.AudioRecord: User permission denied!
D/IpaAndroidAudioManagerFacade: startRecording AudioRecord
D/IPA-Native-I: [IPAStateKeyword::onEntry] publish PLAY_BEEP
W/IPA-Native-W: data received from IPA is NULL! is it ok?
D/IpaCoreSdkAdapter: ipaMessageEventType= PLAY_BEEP
D/IpaAndroidAudioManagerFacade: VOLUME play start + originalVolume=12
D/IpaAndroidAudioManagerFacade: VOLUME play start + streamVolume=7
I/AudioTrack: Skip ramp
D/IpaAndroidAudioManagerFacade: VOLUME play end + originalVolume=12
D/IpaAndroidAudioManagerFacade: VOLUME play end + streamVolume=12
D/IntentGenerator: sendK2KnobIntent patype->5 state-> 1
D/IntentGenerator: Complete sendK2KnobIntent patype->5 state-> 1
D/IPA-Native-I: [IPAStateKeyword::onEntry] Generating internal event...

Upvotes: 1

Views: 712

Answers (3)

drc
drc

Reputation: 275

Samsung (and possibly other phones) do not allow "dangerous" actions such as RECORD_AUDIO to be invoked unless they are coming from a Thread your process owns. This means that if you are trying to invoke from a Binder thread or other IPC thread, the request will fail with the permission error, even if your app has accepted all relevant permissions. It's a good security measure, but they could add some logging b/c it's unclear why the failure.

Try spawning another thread via a Handler() or using Activiy.runOnUiThread() and invoking startRecord() there.

Upvotes: 0

SMARAK DAS
SMARAK DAS

Reputation: 1

Start the startRecording() method of AudioRecord class with some delay after creating the Audiorecord instance:

        public void start() {
        // Stop recording if it is currently ongoing.
                stop();
                // Try to create a new recording session.
                mAudioRecord = createAudioRecord();
                CommonMethods.printLog(TAG, mAudioRecord.toString());
                if (mAudioRecord == null) {
                    throw new RuntimeException("Cannot instantiate VoiceRecorder");
                }
                // Start recording.

                if (getDeviceManufacturer().contains("samsung")) {
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            new CountDownTimer(100, 50) {
                                @Override
                                public void onTick(long millisUntilFinished) {

                                }

                                @Override
                                public void onFinish() {
                                    mAudioRecord.startRecording();
                                    // Start processing the captured audio.
                                    mThread = new Thread(new ProcessVoice());
                                    mThread.start();
                                }
                            }.start();
                        }
                    });
                } 
            }

     private AudioRecord createAudioRecord() {
            for (int sampleRate : SAMPLE_RATE_CANDIDATES) {
                final int sizeInBytes = AudioRecord.getMinBufferSize(sampleRate, CHANNEL, ENCODING);
                if (sizeInBytes == AudioRecord.ERROR_BAD_VALUE) {
                    continue;
                }
                final AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                        sampleRate, CHANNEL, ENCODING, sizeInBytes);
                if (audioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
                    mBuffer = new byte[sizeInBytes];
                    return audioRecord;
                } else {
                    audioRecord.release();
                }
            }
            return null;
        }

 private String getDeviceManufacturer() {
        String deviceManufacturer = android.os.Build.MANUFACTURER;
        return deviceManufacturer;
    }

Upvotes: 0

Sz-Nika Janos
Sz-Nika Janos

Reputation: 821

Use PermissionChecker.checkCallingOrSelfPermission it should do the job.

if (PermissionChecker.checkCallingOrSelfPermission(this, Manifest.permission.RECORD_AUDIO)
            == PackageManager.PERMISSION_GRANTED) {

       //Start recognition

    } else if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.RECORD_AUDIO)) {

            // show permission dialog.
        showPermissionMessageDialog();
    } else {

         //Request permission.
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},
                REQUEST_RECORD_AUDIO_PERMISSION);
    }

Upvotes: 1

Related Questions