3

你好我有这个代码

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

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

该怎么办?谢谢

4

0 回答 0