I want to read 1024 bytes at a time using FileInputStream, and pass the byte buffer to AudioTrack which will play the 1024 bytes. I have put this is a while loop so that the entire track is played continuously. You might suggest that you could read the entire PCM at once, but I do not want to do that. This snippet of code plays only the first 1024 bytes, and exits the loop after that.
fis = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Notate/" + MainActivity.filepath);
l = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Notate/" + MainActivity.filepath);
byte[] buffer = new byte[1024];
try {
fis = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Notate/" + MainActivity.filepath);
while (fis.read(buffer)!=-1) {
int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);
TextView t1 = (TextView) contentView.findViewById(R.id.textView1);
t1.setText(Integer.toString(intSize));
if (at != null) {
at.play();
at.write(buffer, 0, buffer.length);
at.stop();
at.release();
}
}
fis.close();
} catch (Exception e) {
}