Search code examples
androidaudiorecord

AudioRecord always create failed


I've been having an issue with using AudioRecord for Android. I've read as much as I can find online about it, but I cannot seem to get a good initialization.

My code to create an AudioRecord object is like this:

    int bufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    if (bufferSize < 4096)
        bufferSize = 4096;

    mBuffer = new short[bufferSize];
    audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize);

and I had acquired the correct permission:

but whenever I run the app, it will show this error:

08-12 11:56:05.669: E/AudioRecord(10689): Could not get audio input for record source 1

08-12 11:56:05.669: E/AudioRecord-JNI(10689): Error creating AudioRecord instance: initialization check failed.

08-12 11:56:05.669: E/AudioRecord-Java(10689): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.



Solution

  • So here is what you have to do to record audio files in android. First you have to add some permissions to your Androidmanifest.xml file. The application needs to have the permission to write to external storage Audio recording for android

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

    Next you have to import the required classes and functions

    import android.app.Activity;
    import android.widget.LinearLayout;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.content.Context;
    import android.util.Log;
    import android.media.MediaRecorder;
    import android.media.MediaPlayer;
    
    import java.io.IOException;
    

    Create an instance of the Android.media.MediaRecorder.

    private void startRecording() {
    
     mRecorder = new MediaRecorder();
    

    Next you have to set the source for the audio input, in most of the cases you would be using the MIC of your android device to give audio input to your application for recording audio files in android. In this case you would have to set audio source to : MediaRecorder.AudioSource.MIC

    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    

    Next you have to define the output format for your recorder file. Android supports a variety for formats for audio. For this you have to use MediaRecorder.SetOutputFormat(); function.

     mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    

    Now you have to name a file which will hold your audio recording in your specified format. For that you have to use MediaRecorder.SetOutputfile() function.

    mRecorder.setOutputFile(mFileName);
    

    Now set the audio encoder using MediaRecorder.setAudioEncoder() function.

    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    

    To preapre your application for recording the audio input call MediaRecorder.prepare()

     mPlayer.prepare();
                mPlayer.start();
            } catch (IOException e) {
                Log.e(LOG_TAG, "prepare() failed");
            }
        }
    

    Now everything is set up and ready, all you have to do is initiate the audio capturing process. For that you will have to do this: MediaRecorder.start();

    class RecordButton extends Button {
            boolean mStartRecording = true;
    
            OnClickListener clicker = new OnClickListener() {
                public void onClick(View v) {
                    onRecord(mStartRecording);
                    if (mStartRecording) {
                        setText("Stop recording");
                    } else {
                        setText("Start recording");
                    }
                    mStartRecording = !mStartRecording;
                }
            };
    
            public RecordButton(Context ctx) {
                super(ctx);
                setText("Start recording");
                setOnClickListener(clicker);
            }
        }
    

    To stop the recording, call MediaRecorder.stop() At the end, when you are done with recording you have to release the resources byt calling MediaRecorder.release()

    mPlayer.release();
    

    Below is an example of application that records an audio and then plays it back.

    package com.android.audiorecordtest;
    
    import android.app.Activity;
    import android.widget.LinearLayout;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.content.Context;
    import android.util.Log;
    import android.media.MediaRecorder;
    import android.media.MediaPlayer;
    
    import java.io.IOException;
    
    public class AudioRecordTest extends Activity
    {
        private static final String LOG_TAG = "AudioRecordTest";
        private static String mFileName = null;
    
        private RecordButton mRecordButton = null;
        private MediaRecorder mRecorder = null;
    
        private PlayButton   mPlayButton = null;
        private MediaPlayer   mPlayer = null;
    
        private void onRecord(boolean start) {
            if (start) {
                startRecording();
            } else {
                stopRecording();
            }
        }
    
        private void onPlay(boolean start) {
            if (start) {
                startPlaying();
            } else {
                stopPlaying();
            }
        }
    
        private void startPlaying() {
            mPlayer = new MediaPlayer();
            try {
                mPlayer.setDataSource(mFileName);
                mPlayer.prepare();
                mPlayer.start();
            } catch (IOException e) {
                Log.e(LOG_TAG, "prepare() failed");
            }
        }
    
        private void stopPlaying() {
            mPlayer.release();
            mPlayer = null;
        }
    
        private void startRecording() {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setOutputFile(mFileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    
            try {
                mRecorder.prepare();
            } catch (IOException e) {
                Log.e(LOG_TAG, "prepare() failed");
            }
    
            mRecorder.start();
        }
    
        private void stopRecording() {
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }
    
        class RecordButton extends Button {
            boolean mStartRecording = true;
    
            OnClickListener clicker = new OnClickListener() {
                public void onClick(View v) {
                    onRecord(mStartRecording);
                    if (mStartRecording) {
                        setText("Stop recording");
                    } else {
                        setText("Start recording");
                    }
                    mStartRecording = !mStartRecording;
                }
            };
    
            public RecordButton(Context ctx) {
                super(ctx);
                setText("Start recording");
                setOnClickListener(clicker);
            }
        }
    
        class PlayButton extends Button {
            boolean mStartPlaying = true;
    
            OnClickListener clicker = new OnClickListener() {
                public void onClick(View v) {
                    onPlay(mStartPlaying);
                    if (mStartPlaying) {
                        setText("Stop playing");
                    } else {
                        setText("Start playing");
                    }
                    mStartPlaying = !mStartPlaying;
                }
            };
    
            public PlayButton(Context ctx) {
                super(ctx);
                setText("Start playing");
                setOnClickListener(clicker);
            }
        }
    
        public AudioRecordTest() {
            mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
            mFileName += "/audiorecordtest.3gp";
        }
    
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
    
            LinearLayout ll = new LinearLayout(this);
            mRecordButton = new RecordButton(this);
            ll.addView(mRecordButton,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    0));
            mPlayButton = new PlayButton(this);
            ll.addView(mPlayButton,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    0));
            setContentView(ll);
        }
    
        @Override
        public void onPause() {
            super.onPause();
            if (mRecorder != null) {
                mRecorder.release();
                mRecorder = null;
            }
    
            if (mPlayer != null) {
                mPlayer.release();
                mPlayer = null;
            }
        }
    }
    

    or use this amazing tutorial