Skip to content Skip to sidebar Skip to footer

How To Record Audio Using Audio Api In Android?

I have an LG Android Phone with Version 2.3.3. I need to connect a microphone in the headphone jack because I want to create an app that reads sound samples. How can I read sampl

Solution 1:

If you really want to read AudioSamples, I would suggest you to use AudioRecord instead of MediaRecorder since it gives you more control on AudioSamples... For that you can use following code, AudioCapturer is my wrapper class which I use for getting the samples from AudioRecord object..IAudioReceiver is an interface which has methods for handling audio data.

publicclassAudioCapturerimplementsRunnable {

    privateAudioRecordaudioRecorder=null;
    privateint bufferSize;
    privateintsamplePerSec=16000;
    privateStringLOG_TAG="AudioCapturer";
    privateThreadthread=null;

    privateboolean isRecording;
    privatestatic AudioCapturer audioCapturer;

    private IAudioReceiver iAudioReceiver;

    privateAudioCapturer(IAudioReceiver audioReceiver) {
        this.iAudioReceiver = audioReceiver;
    }

    publicstatic AudioCapturer getInstance(IAudioReceiver audioReceiver) {
        if (audioCapturer == null) {
            audioCapturer = newAudioCapturer(audioReceiver);
        }
        return audioCapturer;
    }

    publicvoidstart() {

        bufferSize = AudioRecord.getMinBufferSize(samplePerSec, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);

        if (bufferSize != AudioRecord.ERROR_BAD_VALUE && bufferSize != AudioRecord.ERROR) {

            audioRecorder = newAudioRecord(MediaRecorder.AudioSource.DEFAULT, this.samplePerSec, AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, this.bufferSize * 10); // bufferSize// 10xif (audioRecorder != null && audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
                Log.i(LOG_TAG, "Audio Recorder created");


                audioRecorder.startRecording();
                isRecording = true;
                thread = newThread(this);
                thread.start();

            } else {
                Log.e(LOG_TAG, "Unable to create AudioRecord instance");
            }

        } else {
            Log.e(LOG_TAG, "Unable to get minimum buffer size");
        }
    }

    publicvoidstop() {
        isRecording = false;
        if (audioRecorder != null) {
            if (audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
                // System.out// .println("Stopping the recorder inside AudioRecorder");
                audioRecorder.stop();
            }
            if (audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
                audioRecorder.release();
            }
        }
    }

    publicbooleanisRecording() {
        return (audioRecorder != null) ? (audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) : false;
    }

    @Overridepublicvoidrun() {
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);  
        while (isRecording && audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
            short[] tempBuf = newshort[Constants.FRAME_SIZE / 2];
            audioRecorder.read(tempBuf, 0, tempBuf.length);     
            iAudioReceiver.capturedAudioReceived(tempBuf, false);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#finalize()
     */@Overrideprotectedvoidfinalize()throws Throwable {
        super.finalize();
        System.out.println("AudioCapturer finalizer");
        if (audioRecorder != null && audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
            audioRecorder.stop();
            audioRecorder.release();
        }
        audioRecorder = null;
            iAudioReceiver = null;  
        thread = null;
    }

}

Now you can use this class's object from the Main class of you program and it will start giving you audio Samples you can handle them inside your IAudioReceiver (class which uses these samples)..

If you still want to use MediaRecorder, this link can be useful to you,

Solution 2:

2) How can i read samples programatically?

That I know of, In Android you can record audio using one of this two classes:

  • MediaRecorder Class

    Used to record audio and video. The recording control is based on a simple state machine

  • AudioRecord class

    The AudioRecord class manages the audio resources for Java applications to record audio from the audio input hardware of the platform. This is achieved by "pulling" (reading) the data from the AudioRecord object. The application is responsible for polling the AudioRecord object in time using one of the following three methods: read(byte[], int, int), read(short[], int, int) or read(ByteBuffer, int). The choice of which method to use will be based on the audio data storage format that is the most convenient for the user of AudioRecord.

Ps: Follow this links above to read and understand the one that best fits your needs.


1) Which Microphone do you recommend?

As I've mentioned on the comment placed on your question, this gets off-topic here on stackoverflow, but for the purpose of completeness:

Post a Comment for "How To Record Audio Using Audio Api In Android?"