I want to play an audio file from an SD card using AudioTrack. I tried with this code:
int minBufferSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); int bufferSize = 512; AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM); int i = 0; byte[] s = new byte[bufferSize]; FileInputStream fin = new FileInputStream(path); DataInputStream dis = new DataInputStream(fin); at.play(); while ((i = dis.read(s, 0, bufferSize)) > -1) { at.write(s, 0, i); } at.stop(); at.release(); dis.close(); fin.close();
But it does not play the audio file properly. Instead of the original sound, it reproduces some kind of noisy sound.
asish source share