Reputation: 294
I have some questions regarding the Android AudioTrack functionalities. I want to know what exactly the blocking does for AudioTrack.
Is it that the write function blocks the thread(say main gui thread) that it is running into or it just prevents AudioTrack from playing the buffer?
When should the play function be called?After writing buffer or before it? right now I am calling track.play() once, and then write buffer via code without calling stop.
Does the buffer size include both right and left interleaved samples or it just means any of the two? say, I have 8192 samples, my min buffer system size is 15000. So Can I define my buffer size as 8192 or should it be twice?
Thanks in advance for your answers.
Upvotes: 1
Views: 1152
Reputation: 21
if you are using AudioTrack.MODE_STREAM, you need to first call play, and then write. for AudioTrack.MODE_STATIC, you call write before play(and then reload static data if you want to play again, or setlooppoints for looping).
as far as blocking during the write, I'm also running into an issue with that. write will create a separate thread to send to the system audio, but as long as you're just using one audio track instance shouldn't be a problem. probably best to just kick off a thread when you are doing your writing.
as far as buffer sizes go, yes the data contains all channels, so if you're using mono samples/files, the length of the sample will be divided by 2. here's one thing I did to get buffer size, although I needed to modify it later on depending on how quickly I wanted the sample to play:
int minBufferSize = AudioTrack.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT);
you can probably set it to anything as long as it seems to perform ok for you. when I encoded the mp3 file/sample(again, for MONO), I had to write to the output PCM array with the total array/2.
Upvotes: 1