Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 420
hisham Avatar asked Dec 11 '22 07:12

hisham


1 Answers

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;
        }
    }
}
like image 118
bko Avatar answered Dec 28 '22 07:12

bko