Hasala
Hasala

Reputation: 126

Record data from mobile phone's audio jack

I am trying to record data from my mobile phone's audio interface. I used audiorecord function. Following is my code:

public void Initialize() {

buffersizebytes = AudioRecord.getMinBufferSize(SAMPPERSEC,channelConfiguration, audioEncoding); // 4096 on ion
buffer = new short[buffersizebytes];
buflen = buffersizebytes / 2;
audioRecord = new AudioRecord(
        android.media.MediaRecorder.AudioSource.MIC, SAMPPERSEC,
        channelConfiguration, audioEncoding, buffersizebytes);
acquire();


for(int i=0; i<4096; i++) buffer[i]=1; 

}

public void acquire() {
    try {
        audioRecord.startRecording();
        mSamplesRead = audioRecord.read(buffer, 0, buffersizebytes);
        audioRecord.stop();
    } catch (Throwable t) {
        // Log.e("AudioRecord", "Recording Failed");
    }
}

I want to put my acquired data into a buffer of 4096 bytes. But my program only put data into 1024 bytes. Also first 432 bytes also zeros. But I am sending data continuously. What could be the issue?

Upvotes: 0

Views: 873

Answers (1)

Vince Moln&#225;r
Vince Moln&#225;r

Reputation: 1062

getMinBufferSize, as the name implies gives you the minimum buffer size. You can set anything bigger, including 4096.

As for the first samples after initialization, my phone gives two gigantic peaks that last for about 0.5 seconds, so I guess it is caused by the recorder starting up. Try skipping a few samples (let's say 500) before processing real data.

Furthermore, the size of buffer should be buffersizebytes/2.

Upvotes: 1

Related Questions