android play sound with AudioTrack

倖福魔咒の 提交于 2019-12-10 18:06:22

问题


hello I have this code

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

its works but there is one problem, when I play sound first time it plays fine, but second and third.. play have sound like "click" (I tried many sound files)

what to do? thanks

来源:https://stackoverflow.com/questions/11203474/android-play-sound-with-audiotrack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!