hisham
hisham

Reputation: 81

startRecording() called on an uninitialized AudioRecord

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();

Upvotes: 8

Views: 12302

Answers (3)

ElyesBRD
ElyesBRD

Reputation: 1

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

Upvotes: 0

Ganesh Moorthy A
Ganesh Moorthy A

Reputation: 204

The device works when the sample rate is set to 22050

Upvotes: 0

bko
bko

Reputation: 1208

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

Upvotes: 17

Related Questions