安卓游戏声音AudioTrack(android play sound with AudioTrac

2019-09-18 13:08发布

你好,我有这样的代码

AudioTrack audioTrack;

public void playAccordeon() {
    int minBufferSize = AudioTrack.getMinBufferSize(44100,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    int bufferSize = 512;
    if (audioTrack == null) {
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT, minBufferSize,
                AudioTrack.MODE_STREAM);
        audioTrack.play();
    }

    int i = 0;
    byte[] s = new byte[bufferSize];
    try {
        InputStream fin = getResources().openRawResource(R.raw.accordeon);

        while ((i = fin.read(s)) != -1) {
            audioTrack.write(s, 0, i);
        }

        fin.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

它的作品,但有一个问题,当我播放声音第一次正常播放,但第二和第三..播放有声如“点击”(我试过很多声音文件)

该怎么办? 谢谢

文章来源: android play sound with AudioTrack