本文介绍了Android的:播放AudioTrack多次生产崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图播放音频的缓冲声音使用AudioTrack(.WAV)。请参阅下面的code。我需要调用这个函数一个主题下,支持同时播放。这是罚款作为一个主题之下。它工作正常播放声音正常。但是,如果我执行使用打AudioTrack一个声音此起彼伏不断(即,在完成第一个播放声音之前执行的第二个上场),生产设备崩溃(强制关闭意外错误)。
有谁碰到过这样的问题,在某种程度上解决这个问题?

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();              

}

鸭preciate你的回应。

Appreciate your response.

感谢。

推荐答案

您必须释放AudioTracks资源以及停止它

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

at.release();

这篇关于Android的:播放AudioTrack多次生产崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 07:03