Android AudioTrack clicks at start and end of sound

后端 未结 2 1296
离开以前
离开以前 2021-01-14 07:07

I get clicks at the start and end of playing a sound (a wav from the sdcard). It must be something to do with the track buffering but I dont know the solution. Also, I creat

相关标签:
2条回答
  • 2021-01-14 07:54

    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.

    0 讨论(0)
  • 2021-01-14 07:55

    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();
    }
    
    0 讨论(0)
提交回复
热议问题