Search code examples
androidaudioseekbar

why sound on and off frequently after add seekbar to audio player?


I've made an Android app. In this app there is a sound player. I've put a Seekbar for audio player. However, when playing audio, the sound is on and off frequently. This problem has been created since adding seekbar.I use Thread for solve this problem but it not solved.I use this codes in OnCreate method :

btn_playSound.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            stop_background_sound();
            if (mPlayer.isPlaying()) {
                mPlayer.pause();
            } else {
                mPlayer.start();
                new Thread() {
                    public void run() {
                        seekBar.post(new Runnable() {

                            @Override
                            public void run() {
                                // seekTo(getCurrentPosition());
                                seekBar.setProgress(mPlayer.getCurrentPosition());
                                seekBar.setMax(mPlayer.getDuration());
                                mHandler.postDelayed(this, 100);

                            }
                        });
                    }
                }.start();
            }
            call_audio_player();
        }
    });



seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            seekTo(progress);
        }
    });

Solution

  • As I expected, you are updating progress of player's playback even when you change the position of seekbar with your handler, not changed by user.
    replace

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        seekTo(progress);
    } 
    

    with

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if(fromUser) {
            seekTo(progress);
        }
    }