AudioTrack does not play the PCM audio recorded using AudioREcord

1,022 views
Skip to first unread message

Jeevitha Jayaraman

unread,
Jan 4, 2011, 4:56:55 PM1/4/11
to Android Music Developers
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.

Alex

unread,
Jan 9, 2011, 3:27:13 PM1/9/11
to Android Music Developers
Hi Jeevitha,

The first thing I can see is that the playback bit is using the small
buffer that you were using for recording. I think you mean to be
reading instead from the accumulated data stream (this may be
something you've done as part of debugging, in which case my
apologies).

If you're concerned that the audio isn't playing back, the quickest
test is to play with the emulator volume controls on the android home
screen. They should make an audible beeping noise. That's usually my
first port of call to check that another app isn't hogging the audio.
I quite often find that it's necessary to close other audio-enabled
apps when running under Linux, but that's probably just Linux ;)

Happy coding,

Alex

Jeevitha Jayaraman

unread,
Jan 10, 2011, 2:25:42 PM1/10/11
to android-musi...@googlegroups.com
 
Alex,
 
Thanks for the reply. Instead of the small buffer I tried accessing from the raw file and it works fine now.
MAy be the buffer had very small audio data to tested.
 
Following is the modified audio output code:
 

File file =

this.fileName;

int

audioLength = (int)(file.length());

byte[] audio = new byte[audiolength];

try {

InputStream is =

new FileInputStream(file);

BufferedInputStream bis =

new BufferedInputStream(is);

DataInputStream dis =

new DataInputStream(bis);

// Read the file into the buffer

int i = 0;

while (dis.available() > 0) {

audio[i] = dis.readByte();

i++;

}

 

// Close the input streams.

dis.close();

 

AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,

8000,

AudioFormat.

CHANNEL_CONFIGURATION_MONO,

AudioFormat.

ENCODING_PCM_16BIT,

audioLength,

AudioTrack.

MODE_STREAM);

audioTrack.play();

str = audioTrack.write(music, 0, audioLength);

}

catch (Throwable t) {

Log.e(

"AudioOutput","Failed");

}

Reply all
Reply to author
Forward
0 new messages