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 samples programmatically?.
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 samples programmatically?.
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.
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'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,
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:
How do i use a external microphone with my Galaxy Nexus?
Any success with external microphones for Android phones or tablets?