I am processing audio recorded by AudioRecord in a thread, but if I run the processing in the recorder thread, some frames get dropped. According to android, AudioRecord.read() --> bufferoverflow, how to handle the buffer?, I need to run the processing in a separate thread, but how? Do I need to create a new thread for every single frame (2-3 per second)?
Here is my current solution but I am wondering if there is a better way to do this?
//in the AudioRecord thread
while (!break_condition) {
int num_read = recorder.read(buffer, 0, buffer.length);
HeavyProcessingRunnable myRunnable = new HeavyProcessingRunnable(buffer);
Thread t = new Thread(myRunnable)
t.start();
}
The runnable for the heavy processing
public class HeavyProcessingRunnable implements Runnable {
private byte[] var;
public HeavyProcessingRunnable(byte[] var) {
this.var = var;
}
public void run() {
//if this is executed in audio recorder thread, frames get dropped
someHeavyProcessing(var);
}
}