Programming/Non programming

Monday, March 29, 2010

Android: Play PCM(.WAV) audio buffer using AudioTrack

Android:

Play Audio sound .PCM(.WAV) buffer using AudioTrack class

The following is the code samples shows this task:

1. First way:

Play the sound directly without reading file with some amount of buffer by buffer. It is
useful to play short sounds.

private void PlayShortAudioFileViaAudioTrack(String filePath) throws IOException
{
// We keep temporarily filePath globally as we have only two sample sounds now..
if (filePath==null)
return;

//Reading the file..
byte[] byteData = null;
File file = null;
file = new File(filePath); // for ex. path= "/sdcard/samplesound.pcm" or "/sdcard/samplesound.wav"
byteData = new byte[(int) file.length()];
FileInputStream in = null;
try {
in = new FileInputStream( file );
in.read( byteData );
in.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Set and push to audio track..
int intSize = android.media.AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT, intSize, AudioTrack.MODE_STREAM);
if (at!=null) {
at.play();
// Write the byte array to the track
at.write(byteData, 0, byteData.length);
at.stop();
at.release();
}
else
Log.d("TCAudio", "audio track is not initialised ");

}


2. Second way:

Play the sound by reading file with some amount of buffer by buffer. It is useful to play
bigger size sounds.
Reading by 512 kb buffer and writing into track.

private void PlayAudioFileViaAudioTrack(String filePath) throws IOException
{
// We keep temporarily filePath globally as we have only two sample sounds now..
if (filePath==null)
return;

int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);

AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);


if (at==null){
Log.d("TCAudio", "audio track is not initialised ");
return;
}

int count = 512 * 1024; // 512 kb
//Reading the file..
byte[] byteData = null;
File file = null;
file = new File(filePath);

byteData = new byte[(int)count];
FileInputStream in = null;
try {
in = new FileInputStream( file );

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int bytesread = 0, ret = 0;
int size = (int) file.length();
at.play();
while (bytesread < size) { ret = in.read( byteData,0, count); if (ret != -1) { // Write the byte array to the track at.write(byteData,0, ret); bytesread += ret; } else break; } in.close(); at.stop(); at.release(); }

Thank you followers !

Cheers,
M.P.Prabakar
Senior Systems Analyst

Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

8 comments:

  1. INFORLINX is a leading provider of consulting services and technologies that enable clients to achieve real business value using state-of-the-art information technology.

    ReplyDelete
  2. Hi There,
    can android play mms protocol online radio's.
    If so how can we achieve it.

    ReplyDelete
  3. Sir i'm new to mobile development, i hv made an application looking at ur code but the .wav file is not played properly it sounds like an untunned radio sounds can u please help me why is it so,is it a device problem or something is wrong in my code.

    ReplyDelete
  4. I think you should skip the first 44 bytes on a WAV...

    ReplyDelete
  5. I truly like to reading your post. Thank you so much for taking the time to share such a nice information.

    mobile developers

    ReplyDelete
  6. Thanks a lot man /..... seriously ...day saver!!!

    ReplyDelete
  7. Well when I start the thread consisting of code given in second way, my app closes all of a sudden. Since the app involves audio playback, i cant test it on emulator. So no way to get where exactly the exception is thrown!
    Do I have to add permissions in manifest file for audio playback using AudioTrack class???
    By the way thanks for the code!

    ReplyDelete
  8. thanks for sharing your informations

    ReplyDelete