How to record audio using audio api in Android?

I have an LG Android phone with version 2.3.3. I need to plug a microphone into the headphone jack because I want to create an application that reads sound samples.

How can I read samples programmatically?

+4
source share
2 answers

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

public class AudioCapturer implements Runnable { private AudioRecord audioRecorder = null; private int bufferSize; private int samplePerSec = 16000; private String LOG_TAG = "AudioCapturer"; private Thread thread = null; private boolean isRecording; private static AudioCapturer audioCapturer; private IAudioReceiver iAudioReceiver; private AudioCapturer(IAudioReceiver audioReceiver) { this.iAudioReceiver = audioReceiver; } public static AudioCapturer getInstance(IAudioReceiver audioReceiver) { if (audioCapturer == null) { audioCapturer = new AudioCapturer(audioReceiver); } return audioCapturer; } public void start() { bufferSize = AudioRecord.getMinBufferSize(samplePerSec, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); if (bufferSize != AudioRecord.ERROR_BAD_VALUE && bufferSize != AudioRecord.ERROR) { audioRecorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, this.samplePerSec, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, this.bufferSize * 10); // bufferSize // 10x if (audioRecorder != null && audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) { Log.i(LOG_TAG, "Audio Recorder created"); audioRecorder.startRecording(); isRecording = true; thread = new Thread(this); thread.start(); } else { Log.e(LOG_TAG, "Unable to create AudioRecord instance"); } } else { Log.e(LOG_TAG, "Unable to get minimum buffer size"); } } public void stop() { 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(); } } } public boolean isRecording() { return (audioRecorder != null) ? (audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) : false; } @Override public void run() { android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); while (isRecording && audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) { short[] tempBuf = new short[Constants.FRAME_SIZE / 2]; audioRecorder.read(tempBuf, 0, tempBuf.length); iAudioReceiver.capturedAudioReceived(tempBuf, false); } } /* * (non-Javadoc) * * @see java.lang.Object#finalize() */ @Override protected void finalize() 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 object from the main class of your program, and it will start giving you audio samples that you can process in your IAudioReceiver (a class that uses these samples).

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

+6
source

2) How can I read samples programmatically?

What I know in Android you can record audio using one of these two classes:

  • Class MediaRecorder

    Used to record audio and video. Recording management is based on a simple state machine

  • class AudioRecord

    The AudioRecord class manages audio resources for Java applications for recording audio from an audio device on a platform platform. This is achieved by β€œpulling” (reading) data from an AudioRecord object. An application is responsible for polling an AudioRecord object over time using one of the following three methods: read (byte [], int, int), read (short [], int, int) or read (ByteBuffer, int). The choice of method to be used will be based on the audio data storage format that is most convenient for AudioRecord user.

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


1) Which microphone do you recommend?

As I mentioned in the commentary on your question, it gets off topic here on stackoverflow, but for the sake of completeness:

+1
source

All Articles