Android AudioTrack clicks at start and end of sound

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 08:49:59

Isn't it possible that you play the PCM wave file header too?

Each PCM wave file has a small header at the beginning of the file, if you play that, you play the header bytes which could result in a click at te beginning.

I have had these same clicks at the beginning of each track using AudioTrack. I solved it by turning track volume off, waiting half a second, and then restoring normal volume. I no longer have any clicks. Here's the relevant bit of the code.

    at.play();
    at.setStereoVolume (0.0f, 0.0f);

    new Thread (new Runnable(){
        public void run() {
            try{
                Thread.sleep(500);
            } catch (InterruptedException ie) { ; }
            at.setStereoVolume (1.0f, 1.0f);
        }
    }).start();

    new Thread (new Runnable(){
        public void run() {
            int i = 0;
            try{
                buffer = new byte[512];
                while(((i = is.read(buffer)) != -1) && !paused){
                    at.write(buffer, 0, i);
                    position += i;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (!paused){
                parent.trackEnded();
            }
        }
    }).start();
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!