Question

I want to record PCM audio for hours , but I want to save only the last 5 min of recording when the button STOP is pressed. Meaning when START is pushed and the recording has reached 5min , the next step would be to delete the beginning of recording and to append additional read buffer to the recording so the 5 min always remains the same. I need to do this using AudioRecord.

I've tried using AudioRecord class to do 20s recording instead of 5min. I've read bytes into one big byte[] buffer. When START is pressed bytes are read to byte[] buffer and when 20s are reached it begins to overwrite the byte[] buffer from beginning , when 40s are reached it again begins to overwrite byte[] buffer from beginning, when STOP is pushed I rearrange the byte[] buffer into correct sequence and write to file. This method works, but using it I need to allocate memory for byte[] buffer from the start , and 5 min is too big , I hit heap size limit and run out of memory. I'm doing this using sampling rate of 44100Hz.

So, is there a better method to make 5min recording as described above without allocating too much memory ?

Was it helpful?

Solution

If you don't have enough memory, you have two choices.

First choice is to use the disk as buffer, if you have any, this is simple, but if your disk is flash based you'll be wearing it down for no reason.

Second choice is to compress. If you don't need PCM you can encode it as MP3. If you need PCM, there might be other choices. Sound is very redundant and should be quite compressible, even losslessly. But sooner or later you'll reach a limit of your RAM.

OTHER TIPS

Write it to a file instead of keeping it in memory?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top