android mediaRecorder.setAudioSource failed
Asked Answered
C

17

69

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?

Cambridge answered 23/9, 2010 at 21:48 Comment(1)
Time to change the accepted answer...Corrody
I
127

Open "AndroidManifest.xml" ->

add

<uses-permission android:name="android.permission.RECORD_AUDIO" />
Insectivore answered 28/12, 2012 at 6:19 Comment(6)
Geeze, I wish there were some uniformity in the permission names. android.permission.RECORD_AUDIO is great, if there's also android.permission.RECORD_VIDEO, but there's not, there's android.permission.CAMERA which leads obviously to android.permission.MICROPHONE which of course doesn't exist. Curses.Marlyn
Also watch casing. In Android Studio it automatically entered the uppercase name of the constant "ANDROID_PERMISSION.RECORD_AUDIO", but that isn't working!Legitimate
If you forgot this, you may have also forgotten <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> to store the audio file.Corrody
please can you confirm the location of this manifest file on the file system and also if this is still the case for Android 7.1?Marceline
or just accept this permission if running on Android > 6.0 ;)Nievesniflheim
worked for me. For anyone else, you can tell it's working because your device should ask for permission to record audio while using this app. Thanks!Handy
M
41

I know this is a very old question but in Android Marshmallow you have to go on "Settings > Apps > Your App > Permissions" and enble Microphone permission.

Myrmeco answered 18/11, 2015 at 0:10 Comment(1)
Information on how to request individual permissions at runtime: developer.android.com/training/permissions/requesting.htmlVellavelleity
G
13

If you are running on Android M, then you need to request permissions to record audio on first run. To accomplish this, ask the user if you can record audio when the application starts:

private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 29;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (mContext.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO},
                MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
    } else {
        Log.d("Home", "Already granted access");
        initializeView(v);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("Home", "Permission Granted");
                initializeView(v);
            } else {
                Log.d("Home", "Permission Failed");
                Toast.makeText(getActivity().getBaseContext(), "You must allow permission record audio to your mobile device.", Toast.LENGTH_SHORT).show();
                getActivity().finish();
            }
        }
        // Add additional cases for other permissions you may have asked for
    }
}
Gnu answered 29/11, 2015 at 3:4 Comment(1)
Change READ constant to WRITE.Searle
U
6

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
    }
Upshot answered 19/10, 2017 at 17:57 Comment(0)
G
4

IMPORTANT - during the last few hours I tried to figure out how to check if the MIC is caught by a different application or not. I noticed that if 2 application address to MediaRecorder at the same time a RuntimeException will be tossed and you won't be able to use the mic, unless you restart your device (!!) I don't know if it's the best solution but it worked for me. perhaps it will save some of you a few hours some day..

private void validateMicAvailability() throws MicUnaccessibleException {
    AudioRecord recorder =
        new AudioRecord(AudioSource.MIC, 44100,
                AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_DEFAULT, 44100);
    try{
        if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED ){
            throw new MicUnaccessibleException("Mic didn't successfully initialized");
        }

        recorder.startRecording();
        if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING){
            recorder.stop();
            throw new MicUnaccessibleException("Mic is in use and can't be accessed");
        }
        recorder.stop();
    } finally{
        recorder.release();
        recorder = null;
    }
}
Glynnis answered 20/5, 2012 at 15:49 Comment(0)
S
4

I had this exact problem, this solved it for me:

On the device you use for debugging go to Settings -> Apps -> {your app}. Then make sure all the app's permissions (in this case Audio Recording and Writing External Storage) are permitted.

Apprarently when installing an app not via Google Play Store you don't get asked to grant permissions and on some phones this will result in your debugging app not getting some permissions granted.

Check this even if some permissions in the manifest file are getting recognised (such as internet access) because for me some worked and some got automatically blocked.

Hope this helps.

Sexennial answered 2/11, 2015 at 16:50 Comment(0)
B
4

For API 23+ you need to request the read/write permissions even if they are already in your manifest.

// Check for permissions
int permission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);

// If we don't have permissions, ask user for permissions
if (permission != PackageManager.PERMISSION_GRANTED) {
    String[] PERMISSIONS_STORAGE = {
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE
    }; 
    int REQUEST_EXTERNAL_STORAGE = 1;

    ActivityCompat.requestPermissions(
        getActivity(),
        PERMISSIONS_STORAGE,
        REQUEST_EXTERNAL_STORAGE
    );
}
Boydboyden answered 15/8, 2016 at 11:22 Comment(0)
T
2

Please request Manifest.permission.RECORD_AUDIO permission dynamically for Marshmallow and above version.

Tenotomy answered 1/8, 2016 at 12:21 Comment(0)
C
1

That looks correct. Make sure other other applications aren't already using the MIC. Perhaps give the phone a restart and try again.

Cruet answered 24/9, 2010 at 14:12 Comment(1)
If I can use it twice in parallel? For example my CAMCODER used for life audio&Video stream and I need to record it to my device in parallel, with video i don't have any problems but audio recording to one of results.Dewaynedewberry
G
1

This code worked for me

 mRecorder = new MediaRecorder();
 mRecorder.reset();
 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
Gonfanon answered 21/4, 2014 at 5:58 Comment(0)
H
1

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.

Hylan answered 1/10, 2016 at 14:21 Comment(0)
R
1

try this code it helps you a lot and its understandable .....

public class MainActivity extends Activity {

    MediaRecorder mRecorder;
    MediaPlayer mediaPlayer;
    Button start, stop, play,stop_play;
    String FileName="";
    File outfile ;

    public static final int request_code = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        start=(Button)findViewById(R.id.btn_start_main);
        stop=(Button)findViewById(R.id.btn_stop_main);
        play=(Button)findViewById(R.id.btn_play_main);
        stop_play=(Button)findViewById(R.id.btn_playstop_main);

        if (checkPermissionFromDevice()){

            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FileName= Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+
                            UUID.randomUUID()+"AudioFile.3gp";

                    SetupMediaRecorder();

                    try {
                        mRecorder.prepare();
                        mRecorder.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    start.setEnabled(false);
                    stop.setEnabled(true);
                    play.setEnabled(false);
                    stop_play.setEnabled(false);

                    Toast.makeText(getApplicationContext(),"recording....",Toast.LENGTH_SHORT).show();
                }
            });
            stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mRecorder.stop();
                    play.setEnabled(true);
                    start.setEnabled(true);
                    stop.setEnabled(false);
                    stop_play.setEnabled(false);
                    Toast.makeText(getApplicationContext(),"recording stoped....",Toast.LENGTH_SHORT).show();
                }
            });
            play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mediaPlayer=new MediaPlayer();
                    try {
                        mediaPlayer.setDataSource(FileName);
                        mediaPlayer.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer.start();
                    start.setEnabled(false);
                    stop.setEnabled(false);
                    play.setEnabled(false);
                    stop_play.setEnabled(true);
                    Toast.makeText(getApplicationContext(),"playing..",Toast.LENGTH_SHORT).show();
                }
            });
            stop_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mediaPlayer.stop();
                    SetupMediaRecorder();
                    start.setEnabled(true);
                    stop_play.setEnabled(false);
                    play.setEnabled(false);
                }
            });

        }
        else {

            requestPermissionFromDevice();
        }





    }

    private void SetupMediaRecorder() {
        mRecorder=new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(FileName);
    }

    private void requestPermissionFromDevice() {
        ActivityCompat.requestPermissions(this,new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO},
                request_code);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case request_code:
            {
                if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ){
                    Toast.makeText(getApplicationContext(),"permission granted...",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(getApplicationContext(),"permission denied...",Toast.LENGTH_SHORT).show();
                }
            }
                break;
        }
    }

    private boolean checkPermissionFromDevice() {
        int storage_permission= ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int recorder_permssion=ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO);
    return storage_permission == PackageManager.PERMISSION_GRANTED && recorder_permssion == PackageManager.PERMISSION_GRANTED;
    }
}
Rounding answered 28/2, 2018 at 7:29 Comment(0)
M
1

I moved it to another thread. Everything works.

Here's a code:

new Thread(() -> {
    mediaRecorder.reset();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediaRecorder.setOutputFile("/audiorecordtest.3gp");
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            //do something here
            mediaRecorder.stop();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("Audio","unable to prepare");
        }

}).start();
Monadism answered 6/3, 2018 at 16:11 Comment(0)
S
0

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.

Soil answered 17/1, 2011 at 5:27 Comment(1)
whats your point about giving nested tag ,may be you are talking about the <uses-permission>, on ManifestKarlenekarlens
D
0

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)

Desegregate answered 21/9, 2021 at 20:12 Comment(0)
S
-1

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
Stoughton answered 26/2, 2019 at 17:58 Comment(0)
R
-16

Change

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

to

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

Worked for me.

Ritenuto answered 23/3, 2013 at 2:26 Comment(2)
Those evaluate to the same thing. The first is self-closing.Rhettrhetta
@Ritenuto What the hell you are doing here, this is sameFyrd

© 2022 - 2024 — McMap. All rights reserved.