본문 바로가기
개발/안드로이드

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

by darksilber 2012. 2. 6.
반응형
출처 - http://www.mfamstory.com/

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

=============================================================================================================================

public class AndroidAudioDevice

{

AudioTrack track;

short[] buffer = new short[1024];



public AndroidAudioDevice( )

{

int minSize =AudioTrack.getMinBufferSize( 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT );

track = new AudioTrack( AudioManager.STREAM_MUSIC, 44100,

AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,

minSize, AudioTrack.MODE_STREAM);

track.play();

}



public void writeSamples(float[] samples)

{

fillBuffer( samples );

track.write( buffer, 0, samples.length );

}



private void fillBuffer( float[] samples )

{

if( buffer.length < samples.length )

buffer = new short[samples.length];



for( int i = 0; i < samples.length; i++ )

buffer[i] = (short)(samples[i] * Short.MAX_VALUE);;

}

반응형

댓글