I am using AudioTrack library to play pcm samples that are being streamed via wifi. The problem is the playback is very choppy while playing on Lenovo K900 (4.1.2), whereas it is seamless while playing on OnePlusOne (Marshmallow 6.0.1 cyanogen) handset. To rule out streaming delay I first loaded the entire stream in an array then played the audio by looping over the pre-populated array. Still things do not improve at all. Now K900 is running Android 4.1.2 whereas the android sdk I used is for API23, I mean when I open the definition of AudioTrack class it open the class file of API23. Is it causing any problem?
int bufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_8BIT);
if (bufferSize == AudioTrack.ERROR || bufferSize == AudioTrack.ERROR_BAD_VALUE) {
bufferSize = SAMPLE_RATE * 2;
}
int readBytes;
int bufSize=2123880;//size of the file being streamed
byte[] buffer=new byte[bufSize];
//bufferSize*=2;
DataInputStream in = new DataInputStream(socket.getInputStream());
readBytes=0;
while(true) {
try {
if(in.available()>0){
readBytes+=in.read(buffer, readBytes, in.available());
// audioTrack.write(buffer,0,readBytes);
}else{
break;
}
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
//audioTrack.release();
break;
} catch (IOException e) {
e.printStackTrace();
//audioTrack.release();
break;
}
}
in.close();
readBytes=0;
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
SAMPLE_RATE,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_8BIT,
bufferSize,
AudioTrack.MODE_STREAM);
audioTrack.play();
while(readBytes<bufSize){
readBytes+=audioTrack.write(buffer,readBytes,bufferSize);
}
audioTrack.release();
I tested with samples having different sampling rate ranging from 19250hz to 44100hz. For all cases K900 strong text is playing choppy whereas Oneplus one plays smooth. I dont think that K900 is underpowered to pull up this easy job. Kindly assist.
EDIT Here is the code I intend to use:
try {
//dataInputStream = new DataInputStream(socket.getInputStream());
//response = dataInputStream.readUTF();
int bufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_8BIT);
if (bufferSize == AudioTrack.ERROR || bufferSize == AudioTrack.ERROR_BAD_VALUE) {
bufferSize = SAMPLE_RATE * 2;
}
int readBytes;
byte[] buffer = new byte[bufferSize];
bufferSize = bufferSize * 2;
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
SAMPLE_RATE,
AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_8BIT,
bufferSize,
AudioTrack.MODE_STREAM);
audioTrack.play();
DataInputStream in = new DataInputStream(socket.getInputStream());
while (true) {
try {
if (in.available() > 0) {
readBytes = in.read(buffer, 0, buffer.length);
audioTrack.write(buffer, 0, readBytes);
}
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
audioTrack.release();
in.close();
socket.close();
break;
} catch (IOException e) {
e.printStackTrace();
audioTrack.release();
in.close();
socket.close();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Thanks, Debojit