Problem playing audio file using AudioTrack:

All your problems with Audio, Video and Images.

Problem playing audio file using AudioTrack:

Postby John » Mon Mar 15, 2010 8:46 pm

0 vote down star


Hello All,

I want to play buffer audio from pcm or wav format files programmatically. I use AudioTrack API for that. But the problem which i'm observing is, it plays properly for lowest audio bitrate files, for ex: if i have a .wav file which has 88,000 audio bit rate, it pays well, otherwise if i have any other files more than this bitrate (or) bigger Size wav file, it is giving some junk sound instead of the proper audio sound. What is the problem here? AudioTrack should be able to play only lower bitrate files? (or) Do i need to modify anything in the code? Could someone suggest me?

Code: Select all
private void PlayMyAudio(String filePath) throws IOException
{

    byte[] byteData;

    File file = new File(filePath); // it is a .wav file
    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_16BIT);

    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);

    at.play();

    // Write the byte array to the track
    at.write(byteData, 0, byteData.length);
    at.stop();         
}
John
Freshman
Freshman
 
Posts: 7
Joined: Fri Oct 16, 2009 8:36 am

Top

Postby mrco » Tue Mar 16, 2010 8:37 am

if i have a .wav file which has 88,000 audio bit rate
88kHz? Are you sure?

AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);

In this code you creating 8kHz AudioTrack.
mrco
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Jan 15, 2010 11:31 am
Location: 2V Software

Postby John » Tue Mar 16, 2010 12:28 pm

Yes, 88kHz. It plays any sound which has lower audio bit rate files, for ex: <88,000. But if the file is more than that, it doesn't play the sound properly, instead it produces some unexpected sound for the sound file.
Creating 8kHz AudioTrack is minimum size right?
John
Freshman
Freshman
 
Posts: 7
Joined: Fri Oct 16, 2009 8:36 am

Postby mrco » Tue Mar 16, 2010 2:32 pm

John wrote:Yes, 88kHz. It plays any sound which has lower audio bit rate files, for ex: <88,000. But if the file is more than that, it doesn't play the sound properly, instead it produces some unexpected sound for the sound file.
Creating 8kHz AudioTrack is minimum size right?

Right, but AudioTrack samplerate should be the same as of your PCM data. And highest is 44kHz (and on some devices may be 48kHz).
mrco
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Jan 15, 2010 11:31 am
Location: 2V Software

Postby mrco » Tue Mar 16, 2010 2:39 pm

Also check, may be other files have 2 channels or another PCM encoding.
mrco
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Jan 15, 2010 11:31 am
Location: 2V Software

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 23 guests