用AudioTrack播放安卓音效

时间:2012-06-26 08:32:08

标签: android audio-streaming android-audiomanager

你好我有这个代码

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

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

该怎么办?感谢

0 个答案:

没有答案