startRecording() called on an uninitialized AudioRecord
Asked Answered
J

3

8

i am trying to record voice call on android. I am using AudioRecord class/api of android to perform this. But for some reason AudioRecord is not able to record voice call on some devices (especially with latest OS 6.0, 7.0). Whenever i set the AudioSource param of AudioRecord object to "VOICE_CALL" i.e (MediaRecorder.AudioSource.VOICE_CALL), it gives me this exception

java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord

But when i set the audiosource to "MIC", it works fine, but of course not records the voice call.

I've tried to use MediaRecord class of android but faced the same issue i.e works fine for "MIC" but lacks on "VOICE_CALL". I also tried many available solutions on multiple forums as well but still no luck.

Below i shared a little piece of my code. Any help on this will be much appreciated. Thanks

    recorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_CALL,
            44100, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT, AudioRecord.getMinBufferSize(44100,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT));
    recorder.startRecording();
Joanajoane answered 4/4, 2018 at 14:29 Comment(0)
S
17

You need to explicitly ask for:

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

on all devices after Lollipop so API lvl 23+

if (ContextCompat.checkSelfPermission(thisActivity, 
    Manifest.permission.RECORD_AUDIO)
        != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.RECORD_AUDIO},
            1234);
}

and then override:

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1234: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                initializePlayerAndStartRecording();

            } else {
                Log.d("TAG", "permission denied by user");
            }
            return;
        }
    }
}
Soapsuds answered 4/4, 2018 at 14:54 Comment(1)
Thanks for showing concern, i already handled permissions for this, so there is no issue of "Record_Audio" permissionJoanajoane
O
0

The device works when the sample rate is set to 22050

Ommatophore answered 13/2 at 6:12 Comment(0)
F
0

SOLUTION: i just got this issue, i simply added permission to use the microphone for the app from my phone's settings.

Franklinfranklinite answered 28/6 at 7:34 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Coercion

© 2022 - 2024 — McMap. All rights reserved.