Hi,
I am new to Android audio stuff. I basically tried to record an audio
using AudioRecord to a byte buffer and then I am trying to play the
same audio from bytebuffer using AudioTrack. I could succesfully
record the audio input from microphone attached to my computer(Tested
it by writing byte buffer to a file and could play it in audacity).
But I am not able to hear anything in the speaker or earphones of my
computer when playing it using AudioTrack. .
Following is the code part for Audio Input
bufferRead = 0;
private static final int audioEncoding =
AudioFormat.ENCODING_PCM_16BIT;
this.setFrequency(8000);
this.setChannelConfiguration(AudioFormat.CHANNEL_CONFIGURATION_MONO);
this.setPaused(false);
int bufferSize = AudioRecord.getMinBufferSize(this.getFrequency(),
this.getChannelConfiguration(), this.getAudioEncoding());
AudioRecord recordInstance = new
AudioRecord(MediaRecorder.AudioSource.MIC, this.getFrequency(), this
.getChannelConfiguration(), this.getAudioEncoding(),
bufferSize);
tempBuffer = new byte[bufferSize];
recordInstance.startRecording();
while (this.isRecording) {
// Are we paused?
synchronized (mutex) {
if (this.isPaused) {
try {
mutex.wait(250);
} catch (InterruptedException e) {
throw new IllegalStateException("Wait() interrupted",e);
}
continue;
}
}
bufferRead = recordInstance.read(tempBuffer, 0, bufferSize);
if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
throw new IllegalStateException("read() returned
AudioRecord.ERROR_INVALID_OPERATION");
} else if (bufferRead == AudioRecord.ERROR_BAD_VALUE) {
throw new IllegalStateException("read() returned
AudioRecord.ERROR_BAD_VALUE");
} else if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
throw new IllegalStateException("read() returned
AudioRecord.ERROR_INVALID_OPERATION");
}
try {
for (int idxBuffer = 0; idxBuffer < bufferRead; ++idxBuffer) {
dataOutputStreamInstance.writeByte(tempBuffer[idxBuffer]);
}
} catch (IOException e) {
throw new
IllegalStateException("dataOutputStreamInstance.writeShort(curVal)");
}
}
// Close resources…
recordInstance.stop();
Code part of Audio Output
try{
int minSize =AudioTrack.getMinBufferSize(this.getFrequency(),
this.getChannelConfiguration(), this.getAudioEncoding());
AudioTrack track = new AudioTrack( AudioManager.STREAM_VOICE_CALL,
this.getFrequency(), this
.getChannelConfiguration(), this.getAudioEncoding(),
minSize, AudioTrack.MODE_STATIC);
int n = track.write( tempBuffer, 0, bufferRead );
track.play();
String str = "Passed and Retrieved"+ n;
return str;
} catch(Exception e){
e.printStackTrace();
return "error";
}
The code gets executed fine without any exceptions and the
track.writ(e...) returns a positive value without any error( 640 in my
case) , I think the audio is actually played and may be its not sent
to computer's audio output. Is there any setting to be done in
emulator to get the audio output to computer speaker or earphones? Any
help in this would be greatly appreciated.