I'm trying to figure how I would use a buffer with AudioTrack to effectively stream music. I know you can queue audio using the write method but once the audio is queued how do you tell how much is left vs. how much has been used/played? Sorry if this is a rudimentary question. I understand the concept of a buffer I'm just not sure how to write one, especially using AudioTrack.
2 Answers
AudioTrack.write() returns "number of bytes written" as an int, and you specify the number of bytes in the buffer when constructing the AudioTrack.
Therefore, to track how much buffer space remains, you could keep an accumulator variable so you know how many bytes have been written in total, and set this variable to zero whenever you call AudioTrack.flush(). However, the linked documentation states "it is typical to use chunks of 1/2 of the total size to permit double-buffering", so it might be simple enough to simply remember if you've written zero, once or twice since calling flush.
To tell how much of the buffer has been played, use AudioTrack.getPlaybackHeadPosition() which returns the number of frames that have been played of the current buffer (i.e. reset to zero on stop, flush or reload) as signed 32-bit integer but to be interpreted as an unsigned 32-bit integer. All this really means is that you assign it to an int as follows.
int bufferPlaybackFrame = myAudioTrack.getPlaybackHeadPosition() & 0xFF;
You can think of frames as equivalent to samples. i.e. You can work out from the AudioFormat used to construct the AudioTrack how many bits (and hence bytes) are being used per sample.
Finally, in case someone was wondering, you'll never be able to tell how much of the source file or stream is left to play through this object (one reason being it's designed to work with permanent 24/7 streams, with no ending), so if you wanted to make a calculation like you see on some video playing websites, that pause the stream until enough id buffered to see the whole video at your current download rate, you'll have to pass that information in some other way.
-
@Andrew what if i want to download (once) the audio files from server and then the files should be play offline. ? Jul 11, 2015 at 5:35
-
@DevendraSingh then you would use a different class and method all together (I don't have an intuition as to which). This is for accessing constant, unending, 24/7 streams that you can't really download and play offline. Jul 12, 2015 at 16:58
-
I don't think frames are equivalent to samples. Frames are equivalent to the number of samples if the output is mono. If it's stereo, the number of samples is two times the number of frames because each frame would be one sample for the left channel, one sample for the right channel.– StéphaneJun 20, 2019 at 8:57
*This code will help to play audio with buffer*
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 ");
}
-
2Sorry, I have should have been more specific. I don't have access to the whole file because I'm streaming it, therefore I can't load the whole file into a buffer before playback. Dec 17, 2013 at 1:54