pranay sancheti
pranay sancheti

Reputation: 11

lagging sound when updating seekbar

I am making a audio player but i'm not able to update the Seek bar without any lag. Kindly try this code and help me!

private void updateProgress() {                //To update progress of seekbar
    long currentPosition = mpintro.getCurrentPosition();

        txtstart.setText(String.format("%02d:%02d",
                TimeUnit.MILLISECONDS.toMinutes(mpintro.getCurrentPosition()),
                TimeUnit.MILLISECONDS.toSeconds(mpintro.getCurrentPosition()) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)
                                mpintro.getCurrentPosition()))));

    seekBar.setProgress((int) currentPosition);   //To set seekbar to current position
}

public void playpause()
{
    final int delay = 1000; //milliseconds

    h.postDelayed(new Runnable(){
            public void run() {
                updateProgress();
                h.postDelayed(this, 1000);
            };
            }, delay);
    }

Upvotes: 1

Views: 749

Answers (1)

Saket Sagar
Saket Sagar

Reputation: 125

Change the onProgressChanged function as:

@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean fromUser) {
    if(fromUser) {
        mplayer.seekTo(i);
    }
}

This is because when your updateProgress funtion updates the seekbar, it also triggers the on onProgressChanged function, which again alters the seekbar and the audio lags. So, add the if statement to check if the seekbar is changed only from user.

Upvotes: 6

Related Questions