I want to record sound from a microphone and access it for possible playback in almost real time. I'm not sure how to use the Android AudioRecord class to record some mic sound and have quick access to it.
For the AudioRecord class, the official website says: "the application polls the AudioRecord object in time," and "the size of the buffer being filled determines the length of the record before exceeding the unread data." He later suggested that frequent polling should use a larger buffer. They never show sample code.
One example that I saw in the book uses the AudioRecord class to continuously read a buffer recently filled with live microphone sound, and then the application writes this data to an SD file. The pseudocode looks something like this:
set up AudioRecord object with buffer size and recording format info set up a file and an output stream myAudioRecord.startRecording(); while(isRecording) {
How this code synchronizes its reading with writing speed is unclear - is the logical "isRecording" ordered and ordered elsewhere? It seems that this code can be read too often or too rarely, depending on how long it takes to read and write.
The doc site also says that the AudioRecord class has a nested class called OnRecordPositionUpdateListener, which is defined as an interface. The information tells you that somehow you specify the period during which you want to be notified of the progress of the recording, and the name of your event handler, and the call is automatically made for your event handler at a specified frequency. I think the structure in the pseudocode will be something like -
set target of period update message = myListener set period to be about every 250 ms other code myListener() { if(record button was recently tapped) handle message that another 250 ms of fresh audio is available ie, read it and send it somewhere )
I need to find a specific code that allows me to capture and process microphone sound with a delay of less than 500 ms. Android offers another class called MediaRecorder, but it doesnβt support streaming, and I might want to stream live microphone audio over a Wi-Fi network in near real time. Where can I find some specific examples?
java android android-hardware audiorecord
kenj Dec 24 '10 at 8:48 2010-12-24 08:48
source share