As the very long title suggests, I'm having trouble playing the audio from a audio that I send over the network through PubNunb. What I do is I send the audio while recording from AudioRecord using this code:
AudioConfig audioConfig = getValidSampleRates(AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
buffer = new byte[audioConfig.getBufferSize()];
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, audioConfig.getSampleSize(), AudioFormat.CHANNEL_IN_MONO, AUDIO_FORMAT, audioConfig.getBufferSize());
Recorded data is sent when the user holds the button down:
private class RecorderRunnable implements Runnable {
@Override
public void run() {
while(mRecording) {
Log.d("RECORDER_STATE", "Recording LOOP");
recorder.read(buffer, 0, buffer.length);
String base64EncodedBuffer = Base64.encodeToString(buffer, Base64.NO_WRAP);
pubnub.publish(MainActivity.CHANNEL_ID, base64EncodedBuffer, new Callback() {
@Override
public void successCallback(String channel, Object message) {
super.successCallback(channel, message);
}
});
}
}
}
Receive code:
@Override
public void successCallback(String channel, final Object message) {
byte[] decodedBase64 = Base64.decode(message.toString(), Base64.NO_WRAP);
speaker.write(decodedBase64, 0, decodedBase64.length);
}
Issue:
I get the audio, but I get sounds that are really slow. "Hello" would sound like:
"Hee-*static*-ll-*static*-oo"
To rule out possible causes, I tried immediately playing the audio like this (without the network):
while(mRecording) {
Log.d("RECORDER_STATE", "Recording LOOP");
recorder.read(buffer, 0, buffer.length);
String base64EncodedBuffer = Base64.encodeToString(buffer, Base64.NO_WRAP);
byte[] decodedBase64 = Base64.decode(base64EncodedBuffer, Base64.NO_WRAP);
speaker.write(decodedBase64, 0, decodedBase64.length);
}
(Note: I did the convert to base64 and back to byte array on purpose.)
The result for the code above (directly playing it after recording) is pretty good. So I'm wondering what I'm doing wrong when handling it over the network.
Any suggestion is appreciated. Thank you.
Edit: 08/28/2015
Found a good explanation for this here. But now the question is, what's the best way of handling network jitter/buffering and packetloss using my current implementation.
See Question&Answers more detail:
os