My first experiment with audio, so this might be not the smartest question. But I'm trying to record audio from the mic and play it back through either a headset or speaker. And making the playbackrate half the HZ works just fine, but making it twice as much makes the audio sound like it's coming in chunks. I'm assuming there is not enough to play, I played with sleep and expanding the record buffer size, but without success.
Does anybody know how I can fix that issue?
I have a buffer of 8000 and the hz=8000 for example, while pbhz=11025.
//audio configuration and SCO Bluetooth connection.
android.os.Process.setThreadPriority(
android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
aManager.startBluetoothSco();
aManager.setBluetoothScoOn(true);
aManager.setMode(AudioManager.MODE_NORMAL);
arec = new AudioRecord(
MediaRecorder.AudioSource.VOICE_RECOGNITION,
hz,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
buffersize);
if (AcousticEchoCanceler.isAvailable()) {
AcousticEchoCanceler echoCanceller = AcousticEchoCanceler.create(arec.getAudioSessionId());
if (echoCanceller != null) {
// echoCanceller.setEnableStatusListener(this);
echoCanceller.setEnabled(true);
}
} else { Log.e("configAudio", "Echo Canceler not available");}
// Creates noise suppressor if available
if (NoiseSuppressor.isAvailable()) {
NoiseSuppressor noiseSuppressor = NoiseSuppressor.create(arec.getAudioSessionId());
if (noiseSuppressor != null) {
// noiseSuppressor.setEnableStatusListener(this);
noiseSuppressor.setEnabled(true);
}
} else { Log.e("configAudio", "Noise Suppressor not available");}
atrack = new AudioTrack(AudioManager.STREAM_MUSIC,
hz,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT,
buffersize,
AudioTrack.MODE_STREAM);
atrack.setPlaybackRate(pbhz);
}
arec.startRecording();
atrack.play();
Runnable runnable = new Runnable() {
@Override
public void run() {
while (isRecording) {
arec.read(buffer, 0, buffersize);
atrack.write(buffer, 0, buffer.length);
}
}
};
new Thread(runnable).start();
When you enable SCO you need to use STREAM_VOICE_CALL, not STREAM_MUSIC for playing sounds.