• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

anyone was able to use ENCODING_PCM_8BIT?

sarah.kho

Lurker
Nov 3, 2010
4
0
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:



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.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones