http://stackoverflow.com/questions/1879 ... ack-arrghh
EDIT: More important question!
Regarding this....
How can I write this 192 size buffer every 4.353ms when I can only retrieve system time in format long. Do I use System.nanoTime()?The only proper way to sequence audio samples is to measure the time by the processed samples, or in other words by the audio buffers you're filling. Then send the final byte buffers to the AudioTrack class (when you're using java). E.g. when you process 192 samples in an environment that runs on 44100hz, you can calculate the passed time by 1000ms / 44100 * 192 = 4.354ms
I wrote this code to play a sample continuously using audiotrack. I calculate the time using nanoSecond. Its plays the sample back quite distorted.
Code: Select all
public void run() {
// TODO Auto-generated method stub
int bufferSize=1024;
byte[] output = new byte[bufferSize];
long period =bufferSize*(1000000000/44100);//in nanoSeconds
int pos1=0;
int pos2=0;
time=System.nanoTime();
while(play)
{
if(pos1<bufferSize)
{
output[pos1]=sample[pos2];
pos1++;
pos2++;
if(pos2>=sample.length)
{
pos2=0;
}
}
if(System.nanoTime()-time>period)
{
time=System.nanoTime();
track.write(output, 0, bufferSize);
pos1=0;
}
}
}