rajan_saini.98
rajan_saini.98

Reputation: 148

Glitches while playing audio in Android

I am using MediaPlayer to play audio in Android. MediaPlayer is working perfectly but when I use a SeekBar which is updated with the duration of audio, the audio doesn't play smoothly and breaks while playing. Below is my code snippet for reference.

mSongSeekBar.setMax(mMediaPlayer.getDuration());
    mSongSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            mMediaPlayer.seekTo(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            mSongSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
        }
    }, 0, 500);

What is the reason for this behavior? How to get rid of this? Should I go with any alternative of Timer?

Upvotes: 0

Views: 184

Answers (3)

Sumit Rajpal
Sumit Rajpal

Reputation: 21

Reason

Audio is not playing continuously, it is changing every 500 millisecond, and so it feels breaking. Because Timer() is changing the value of Progress integer. And mMediaPlayer seeks to it every time automatically, as the code is written:

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

How to Get rid of this

You should use boolean fromUser to change position of audio, only when user changes seekBar manually, You can do this by just a little change(using if)

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

Upvotes: 1

Fasi Hussaini
Fasi Hussaini

Reputation: 36

I had the same problem where the audio was continuously seeking by itself and causing glitches. You need to make sure the audio is seeked only when the user scrubs it and not when it is normally running. For this purpose the boolean fromUser parameter of onProgressChanged is useful. This would ensure the audio seeking only when the user scrubs it.

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

Upvotes: 2

Reaz Murshed
Reaz Murshed

Reputation: 24211

You might consider overriding the onStartTrackingTouch and onStopTrackingTouch methods as well. Because when you are dragging the seek-bar the onProgressChanged is called frequently and each time it is trying to set the media player to play from a specific position and hence you are getting that cracking or breaking of your audio.

I would recommend, pausing the media player temporarily while the seek-bar being dragged and resume the audio when you are done with the dragging. The methods mentioned above might help you to achieve those behaviors.

Upvotes: 0

Related Questions