Android:Playing bigger size audio wav sound file produces crash

Posted by user187532 on Stack Overflow See other posts from Stack Overflow or by user187532
Published on 2010-03-25T16:56:02Z Indexed on 2010/03/26 9:43 UTC
Read the original article Hit count: 387

Filed under:

Hi Android experts,

I am trying to play the bigger size audio wav file(which is >20 mb) using the following code(AudioTrack) on my Android 1.6 HTC device which basically has less memory. But i found device crash as soon as it executes reading, writing and play. But the same code works fine and plays the lesser size audio wav files(10kb, 20 kb files etc) very well. P.S: I should play PCM(.wav) buffer sound, the reason behind why i use AudioTrack here.

Though my device has lesser memory, how would i read bigger audio files bytes by bytes and play the sound to avoid crashing due to memory constraints.

private void AudioTrackPlayPCM() throws IOException

{

String filePath = "/sdcard/myWav.wav"; // 8 kb file

byte[] byteData = null;
File file = null;

file = new File(filePath);

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();

}

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);

at.play();

at.write(byteData, 0, byteData.length);
at.stop();

at.release();

}

Could someone guide me please to play the AudioTrack code for bigger size wav files?

© Stack Overflow or respective owner

Related posts about android