Hi
I am trying to use AudioFormat.ENCODING_PCM_8BIT with AudioRecord and AudioTrack to record some voice from mic and then play it to the speaker.
Here is my code:
and here is the code portion for playing the content from the buffer
The error I get is an IllegalArgumentException in the line where I am initializing the arec object. Any idea why it is happening?
The same code works fine with ENCODING_PCM_16BIT and I do not know what is wrong with it. I tried using byte[] with no luck.
Thanks.
I am trying to use AudioFormat.ENCODING_PCM_8BIT with AudioRecord and AudioTrack to record some voice from mic and then play it to the speaker.
Here is my code:
Code:
int sampleRate = 11025;
int bufferSize = AudioRecord.getMinBufferSize(11025*2,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT);
AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT, bufferSize);
short[] buffer = new short[bufferSize];
arec.startRecording();
arec.read(buffer, 0, bufferSize);
and here is the code portion for playing the content from the buffer
Code:
AudioTrack atrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT, bufferSize,
AudioTrack.MODE_STREAM);
atrack.setPlaybackRate(sampleRate);
atrack.play();
atrack.write(buffer, 0, buffer.length);
The error I get is an IllegalArgumentException in the line where I am initializing the arec object. Any idea why it is happening?
The same code works fine with ENCODING_PCM_16BIT and I do not know what is wrong with it. I tried using byte[] with no luck.
Thanks.