4

I am trying to play audio buffered sound (.wav) using AudioTrack. Please see the code below. I need to call this function under a Thread to support simultaneous play. It is fine being under a Thread. It is working fine playing the sound normally. But if i execute playing the sound using AudioTrack one after another continuously(i.e. executing second play before completing the first play sound), produces device crash (force close unexpectedly error). Does anyone come across such problems and resolve it in a way?

private void PlayAudioTrack(String filePath) throws IOException
{

    if (filePath==null)
        return;

    byte[] byteData = null;

    File file = null; 
    file = new File(filePath); // sdcard path
    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_8BIT);

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

    at.play();

    at.write(byteData, 0, byteData.length);
    at.stop();              

}

Appreciate your response.

Thanks.

Getsy
  • 4,887
  • 16
  • 78
  • 139

1 Answers1

5

You have to release the AudioTracks resources as well as stopping it

at.release();
Donal Rafferty
  • 19,707
  • 39
  • 114
  • 191
  • Thanks for the reply. Do you mean i should use at.stop(); at.release(); before executing everytime freshly? – Getsy Mar 16 '10 at 12:57
  • I tried releasing it after stopping the play and before starting the play etc scenarios, but the crash is happening as it is when my app is executing play simultaneously. Please provide any sample if you have any. – Getsy Mar 16 '10 at 13:04
  • I currently use release in the onDestroy() method of my application where are you using it? – Donal Rafferty Mar 16 '10 at 15:20
  • Release it in onDestroy() will be called only when we exit our application right. It is not my scenario. I just keep pressing a button, where i kept this audiotrack play code under button, produced crash because it tries to call the play one after another click continuously. Crash is observing only when previous sound play is playing and also create new sound play simultaneously. How to fix it in this case? – Getsy Mar 16 '10 at 16:08
  • It gives error in logcat as follows: AudioFlinger: No more track names available AudioTrack: AudioFlinger could not create track AudioTrack: Error -20 initiliazing AudioTrack Play() called uninitialised AudioTrack I think i should NOT call play, write code lines when no track is found, will resolve my problem. Any idea how to findout when 'no track is found' (or) 'Unininitilised audiotrack' error? – Getsy Mar 16 '10 at 16:55
  • You could just disable the button after the first press so the AudioTrack only calls play once, "AudioTrack Play() called uninitialised AudioTrack" means that when you call play the AudioTrack has not been set up. – Donal Rafferty Mar 16 '10 at 17:36
  • No, i cannot disable the button. I should have simultaneous button click enabled, which is my requirement. – Getsy Mar 16 '10 at 17:37
  • Please let me know how can i catch the errors whether 'Unininitilised audiotrack' (or) 'no track is found' (or) audiotrack error, so based on that i will not call Play() method. I tried try,catch blocks near audiotrack calls, but it doesn't throw any error there. – Getsy Mar 16 '10 at 17:39
  • Do you create a brand new AudioTrack each time the button is pressed? You can use at.getState() to see whether the AudioTrack is initilised or not – Donal Rafferty Mar 18 '10 at 09:07
  • Any further update on this? I am also facing similar issue when AudioTrack is created once and used many times. – JRC Dec 22 '11 at 04:26