I wish to record the microphone audio stream so I can do realtime DSP on it.

I want to do so without having to use threads and without having .read() block while it waits for new audio data.

UPDATE/ANSWER: It's a bug in Android. 4.2.2 still has the problem, but 5.01 IS FIXED! I'm not sure where the divide is but that's the story.

NOTE: Please don't say "Just use threads." Threads are fine but this isn't about them, and the android developers intended for AudioRecord to be fully usable without me having to specify threads and without me having to deal with blocking read(). Thank you!

Here is what I have found:

When the AudioRecord object is initialized, it creates its own internal ring type buffer. When .start() is called, it begins recording to said ring buffer (or whatever kind it really is.)

When .read() is called, it reads either half of bufferSize or the specified number of bytes (whichever is less) and then returns.

If there is more than enough audio samples in the internal buffer, then read() returns instantly with the data. If there is not enough yet, then read() waits till there is, then returns with the data.

.setRecordPositionUpdateListener() can be used to set a Listener, and .setPositionNotificationPeriod() and .setNotificationMarkerPosition() can be used to set the notification Period and Position, respectively.

However, the Listener seems to be never called unless certain requirements are met:

1: The Period or Position must be equal to bufferSize/2 or (bufferSize/2)-1.

2: A .read() must be called before the the Period or Position timer starts counting - in other words, after calling .start() then also call .read(), and each time the Listener is called, call .read() again.

3: .read() must read at least half of buffe

Edit
Report