0

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

Debojit Kundu
  • 111
  • 1
  • 13
  • you have `int bufferSize = AudioTrack.getMinBufferSize(...` so what do you need `int bufSize=2123880;` for? – pskink Aug 04 '16 at 05:29
  • Well I just cached the entire stream content in the buffer by reading the data input stream and then played from that buffer itself to rule out any streaming delay. – Debojit Kundu Aug 04 '16 at 05:44
  • so do not use `AudioTrack.MODE_STREAM`, use `MODE_STATIC` instead – pskink Aug 04 '16 at 05:46
  • Using static is not my objective, i want to play via stream, as it is not playing properly i used this approach to check if this plays well as it would be reading from array instead of stream. Please check the code in the edit part. – Debojit Kundu Aug 04 '16 at 07:14
  • then try [this](http://pastebin.com/raw/tz2Mvh5G) – pskink Aug 04 '16 at 07:21
  • Ok, so you are asking me to try with a wav sample. Well I will create a sample of 44100hz with audacity and try playing that. I will let you know the output. – Debojit Kundu Aug 04 '16 at 11:20
  • I could not get much time to try out this, but fortunately I am able to play and record simultaneously on oneplusone. I mean recording on the phone and sending to other end of socket, and receiving a sample from the other end of the socket – Debojit Kundu Aug 04 '16 at 19:15
  • what does it have to do with choppy audio? – pskink Aug 04 '16 at 19:32
  • Actually my original project is to have full duplex audio transmission over wifi. Now I was stuck with a specific device (K900). I just got rid of that problem using another device (oneplusone). I ran the same program on nexus 4, that also works perfectly (i.e., works on oneplusone+nexus 4, but does not work on K900). So i thought to have the original problem solved, and then work on any device specific issue. – Debojit Kundu Aug 05 '16 at 11:07

0 Answers0