tags:

views:

405

answers:

1

Hello friends,

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.

A: 

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

at.release();
Donal Rafferty
Thanks for the reply. Do you mean i should use at.stop();at.release();before executing everytime freshly?
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.
I currently use release in the onDestroy() method of my application where are you using it?
Donal Rafferty
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?
It gives error in logcat as follows:AudioFlinger: No more track names availableAudioTrack: AudioFlinger could not create track AudioTrack: Error -20 initiliazing AudioTrackPlay() called uninitialised AudioTrackI 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?
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
No, i cannot disable the button. I should have simultaneous button click enabled, which is my requirement.
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.
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