2

I am trying to create an app to stream audio from a local file in one device, to a different device using the Nearby communications API. The problem is that I manage to stream the audio, but the only thing I can hear on the remote device is some sort of non-sense cracking noise. What I´ve read so far is that I need to adjust the value in the minBufferSize I´m using and the value in the sampleRate, but I´ve been trying this and I´haven´t achieved much.

This is my code to send the byte chunks:

AudioTrack speaker;
//Audio Configuration.    
private int sampleRate = 16000;      //How much will be ideal?
private int channelConfig = AudioFormat.CHANNEL_OUT_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    minBufSize=2048;
    speaker = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, 10*minBufSize, AudioTrack.MODE_STREAM);
   }



    final InputStream file;
            final byte [] arrayStream = new byte [minBufSize];
            try {
                file= new FileInputStream (filepath);
                bytesRead = file.read(arrayStream);
                while (bytesRead!=-1) {
                    new Thread(new Runnable() {
                        public void run() {
                            sendMessage(arrayStream);
                        }
                    }).start();
                    bytesRead = file.read(arrayStream);
                }
                Toast.makeText(this, "Mensaje totalmente completado",Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

private void sendMessage(byte [] payload) {

    Nearby.Connections.sendReliableMessage(mGoogleApiClient, mOtherEndpointId, payload);

    //mMessageText.setText(null);
}

And this is the code to receive and playback the message on the remote device:

    @Override
public void onMessageReceived(String endpointId, byte[] payload, boolean isReliable) {
    // A message has been received from a remote endpoint.
    Toast.makeText(this,"Mensaje recibido",Toast.LENGTH_SHORT).show();
    debugLog("onMessageReceived:" + endpointId + ":" + new String(payload));

        playMp3(payload);

}
private void playMp3(final byte[] mp3SoundByteArray) {

    if (isPlaying==false){
        speaker.play();
        isPlaying=true;
    }else{
                //sending data to the Audiotrack obj i.e. speaker
                speaker.write(mp3SoundByteArray, 0, minBufSize);
                Log.d("VR", "Writing buffer content to speaker");

        }
}

Can anyone help me with this??

Thanks!

1 Answers1

0

You need to buffer some audio on the client before trying to play it. Most likely you get some data and play it and the next chunk of data has not arrived in time.

See this post and this post about buffering data and streaming audio.

Community
  • 1
  • 1
jaybers
  • 1,991
  • 13
  • 18