android mediaRecorder.setAudioSource failed

Viewed 74299

I have android G1 firmware 1.6, I am trying to record voice from the app with the follow code.

MediaRecorder recorder = new MediaRecorder();
 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 recorder.setOutputFile(PATH_NAME);
 recorder.prepare();
 recorder.start();  

my manifest.xml has:

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

I got:

09-23 14:41:05.531: ERROR/AndroidRuntime(1718): Uncaught handler: thread main exiting due to uncaught exception
09-23 14:41:05.551: ERROR/AndroidRuntime(1718): java.lang.RuntimeException: setAudioSource failed.
09-23 14:41:05.551: ERROR/AndroidRuntime(1718):     at android.media.MediaRecorder.setAudioSource(Native Method)

how do I record voice properly?

17 Answers

For API 23+, check audio record permission in both Manifest.xml <uses-permission android:name="android.permission.RECORD_AUDIO" /> and in Java Code

if (ActivityCompat.checkSelfPermission(cnt, Manifest.permission.RECORD_AUDIO)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(cnt, arrayOf(
                Manifest.permission.RECORD_AUDIO
        ),0)
        return
    }

As a Cyanogenmod-User I had to

  1. make sure the permissions are defined in the manifest (android-studio)
  2. make sure the privacy guard allowed the app to access the microphone (on the device)
  3. make sure the android system had set the permissions under Settings --> Apps --> Select your app --> Permissions --> Check all permissions (should be the ones requested by the manifest) (on the device)

After that recoding worked for me.

My suggestion might look stupid, but it worked for me :)

Try giving permission as a nested tag. I believe there is an xml parsing issue somewhere with android packaging library.

it work for me :-

Ask for permission after setContentView(R.layout.activity_main)

val permissions = arrayOf(Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE) ActivityCompat.requestPermissions(this, permissions,0)

Make sure the android system had set the permissions on the device, should be the ones requested by the manifest:

  • under Settings --> Apps --> Select your app --> Permissions --> Check all permissions in on
Related