开发者

Android:Playing AudioTrack multiple times producing crash

开发者 https://www.devze.com 2022-12-22 21:40 出处:网络
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.

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.


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

at.release();
0

精彩评论

暂无评论...
验证码 换一张
取 消