AudioTrack and RecordAudio

I have been working on using the android device to record and play back raw audio data. Although there are some higher level libraries, I chose AudioTrack and RecordAudio. I mainly chose these because they deal with raw short[] or byte[] arrays for sound data, and also they do work with older android versions. I would say these libraries are problematic, but have some good utilitarian value.
There are many resources you can search for regarding these two libraries, here is one I started with: http://stackoverflow.com/questions/2413426/playing-an-arbitrary-tone-with-android . This is another good one: http://stackoverflow.com/questions/6714677/how-to-capture-audio-from-my-android-phone-microphone . Notice that there is a method to get the buffer size. I have had better luck sticking to using this buffer rate. Supposedly this is the minimum, but there are many ways to make AudioTrack fail, and this is one of them. Also the byte[] data is stored in pairs, the first the high bits of a word[] 0xff00, and the second the low bits 0x00ff. Doing some bit manipulation it is easy to convert between the two, although AudioTrack handles both. Just keep in mind that this is your sound, and once again, AudioTrack gets tempermental, sometimes it doesn’t work. I am not sure of the standards for these numbers, but you will want to be wary of mixing the two up. There also seems to be some sort of bug in the way playmedia is called.  Sometimes the sound cuts out after a brief second and goes silent.  I believe this has to do with buffer size, or some sort of terminating sequence.  I was reading from one buffer copying to another, and had end of buffer issues.  It had something to do with stopping early to avoid copying past one of the buffer’s end.  This is a bug I am still investigating and may be memory allocation related.  The second http link also mentions the android.permission.RECORD_AUDIO is required in your android.xml file, which is essential. Otherwise these methods work fairly well. I am still figuring out all the bugs in them, but unless you need to deal with raw PCM audio data, I recommend you use another library for playing and recording music. In looking around I did see a method where you can call the phone’s built in recorder with a single easy java line. This will jump your app to a new system activity with the built in GUI record activity your phone provides. It will then save your recording to a file, and try to play the file with the default method.  I recommend you look into MediaRecorder, and other resources to see if they will meet your needs before reverting to the old AudioTrack and RecordAudio.

About Rick Tilley

FSU in computer science from 2012 to 2014. I'm a student there, and am interested in developing Android apps.