title: Android SeekBar: When setProgress then SeekBar progress is jumping to the beginning/middle/end
body: <p>I have a problem of that how to setup seekbar to work correctly when I setProgress upon click. Progress is jumping to the beginning/middle/end depends on where I click on it. How is it possible to solve?</p>
  
  <p>That my attemps:</p>
  
  <pre><code> @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_video_player);
  
      getWindow().setFormat(PixelFormat.TRANSLUCENT);
      videoPath = getIntent().getExtras().getString("videoPath");
      videoView = (VideoView) findViewById(R.id.vvPlayer);
      seekBar = (SeekBar) findViewById(R.id.seekBar);
      seekBar.setOnSeekBarChangeListener(this);
          ....
  }
  
   @Override
      public void onPrepared(MediaPlayer mp) {
      // TODO Auto-generated method stub
      seekBar.setMax(mp.getDuration());
      System.out.println("curpos" + mp.getCurrentPosition());
      new Thread(new Runnable() {
          public void run() {
          try {
              while (mPlayer != null &amp;&amp; mPlayer.getCurrentPosition() &lt; mPlayer.getDuration()) {
              seekBar.setProgress(mPlayer.getCurrentPosition());
              Message msg = new Message();
              int millis = mPlayer.getCurrentPosition();
  
              msg.obj = millis / 1000;
  
              try {
                  Thread.sleep(100);
              } catch (InterruptedException e) {
                  e.printStackTrace();
                  System.out.println("interrupt exeption" + e);
              }
              }
          } catch (Exception e) {
              e.printStackTrace();
              System.out.println("my Exception" + e);
          }
          }
      }).start();
      }
  
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      // TODO Auto-generated method stub
      if (fromUser) {
          Log.e("onProgressChanged: ", Integer.toString(progress));
          mPlayer.seekTo(progress);
      }
  
      }
  </code></pre>
  
  <p><strong>Full Code:</strong></p>
  
  <pre><code>public class VideoPlayerActivity extends Activity implements Callback, OnSeekBarChangeListener, OnTouchListener, OnErrorListener, OnCompletionListener, OnPreparedListener {
  
      MediaPlayer mPlayer;
      AssetFileDescriptor afd;
      VideoView videoView;
      SurfaceHolder holder;
      MediaController mController;
      String videoPath;
      SeekBar seekBar;
      ImageButton mPlay, mStop, mPause;
      TextView mCurDuration, mDuration;
      int position = 0;
      Handler handler = new Handler();
      Runnable r;
      boolean controlsVisibility = true;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_video_player);
  
      getWindow().setFormat(PixelFormat.TRANSLUCENT);
      mDuration = (TextView) findViewById(R.id.tvDuration);
      mCurDuration = (TextView) findViewById(R.id.tvCurDuration);
      videoPath = getIntent().getExtras().getString("videoPath");
      videoView = (VideoView) findViewById(R.id.vvPlayer);
      seekBar = (SeekBar) findViewById(R.id.seekBar);
      seekBar.setOnSeekBarChangeListener(this);
      seekBar.setFocusable(false);
      videoView.setOnTouchListener(this);
      mPlay = (ImageButton) findViewById(R.id.play);
      mPause = (ImageButton) findViewById(R.id.pause);
      mStop = (ImageButton) findViewById(R.id.stop);
      mController = new MediaController(this);
      videoView.setMediaController(mController);
      holder = videoView.getHolder();
      holder.addCallback(this);
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  
      mPlay.setOnClickListener(new OnClickListener() {
          public void onClick(View view) {
          Log.e("Play:", " is Pressed!");
          if (mPlayer == null)
              playVideo();
          mPlayer.start();
          }
      });
      mPause.setOnClickListener(new OnClickListener() {
          public void onClick(View view) {
          Log.e("Pause:", " is Pressed!");
          if (mPlayer != null) {
              handler.removeCallbacks(r);
              controlsVisibility=true;
              AutoHideControls();
              Log.e("mPlayer status: ", "Not Null");
              if (mPlayer.isPlaying())
              Log.e("mPlayer status: ", "Not Null");
              mPlayer.pause();
          }
          }
      });
  
      mStop.setOnClickListener(new OnClickListener() {
          public void onClick(View view) {
          Log.e("Stop:", " is Pressed!");
          if (mPlayer != null) {
              handler.removeCallbacks(r);
              controlsVisibility=true;
              AutoHideControls();
              seekBar.setProgress(0);
              mPlayer.stop();
              mPlayer.release();
              mPlayer = null;
          }
          }
      });
      }
  
      private void playVideo() {
      try {
          if (afd != null &amp;&amp; mPlayer != null) {
          mPlayer.start();
          return;
          }
          Log.e("VideoPath:", videoPath);
          if (videoPath != "") {
          afd = getAssets().openFd(videoPath);
          // "videos/start.mp4"
          mPlayer = new MediaPlayer();
          mPlayer.setOnErrorListener(this);
          mPlayer.setOnCompletionListener(this);
          mPlayer.setOnPreparedListener(this);
          mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
          mPlayer.setScreenOnWhilePlaying(true);
          mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
          mPlayer.setDisplay(holder);
          mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
          mPlayer.prepare();
          mPlayer.start();
          handler.removeCallbacks(r);
          controlsVisibility=true;
          AutoHideControls();
          }
      } catch (IllegalArgumentException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IllegalStateException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      }
  
      public void surfaceCreated(SurfaceHolder holder) {
      runOnUiThread(new Runnable() {
          public void run() {
          playVideo();
          }
      });
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      // TODO Auto-generated method stub
  
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
      // TODO Auto-generated method stub
  
      }
  
      @Override
      public boolean onError(MediaPlayer mp, int what, int extra) {
      Log.e("Debug:", "onError---&gt; what:" + what + "   extra:" + extra);
      if (mPlayer != null) {
          mPlayer.stop();
          mPlayer.release();
          mPlayer = null;
      }
      return true;
      }
  
      @Override
      public void onCompletion(MediaPlayer mp) {
      mPlayer.release();
      finish();
      }
  
      public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; event.getRepeatCount() == 0) {
          if (mPlayer != null) {
          mPlayer.stop();
          mPlayer.release();
          }
          finish();
          return true;
      }
      return super.onKeyDown(keyCode, event);
      }
  
      @Override
      public boolean onTouch(View v, MotionEvent event) {
  
      if (controlsVisibility) {
          controlsVisibility = false;
          mPlay.setVisibility(View.GONE);
          mPause.setVisibility(View.GONE);
          mStop.setVisibility(View.GONE);
          seekBar.setVisibility(View.GONE);
          handler.removeCallbacks(r);
      } else {
          controlsVisibility = true;
          mPlay.setVisibility(View.VISIBLE);
          mPause.setVisibility(View.VISIBLE);
          mStop.setVisibility(View.VISIBLE);
          seekBar.setVisibility(View.VISIBLE);
          AutoHideControls();
      }
  
      return false;
      }
  
      public void AutoHideControls() {
  
      r = new Runnable() {
          public void run() {
          controlsVisibility = false;
          mPlay.setVisibility(View.GONE);
          mPause.setVisibility(View.GONE);
          mStop.setVisibility(View.GONE);
          seekBar.setVisibility(View.GONE);
          }
      };
      handler.removeCallbacks(r);
      handler.postDelayed(r, 4000);
      }
  
      @Override
      public void onPrepared(MediaPlayer mp) {
      seekBar.setMax(mp.getDuration());
      new Thread(new Runnable() {
          public void run() {
          int currentPosition = 0;
              int total = mPlayer.getDuration();
              seekBar.setMax(total);
              while (mPlayer != null &amp;&amp; currentPosition &lt; total) {
                  try {
                      Thread.sleep(1000);
                      currentPosition = mPlayer.getCurrentPosition();
                  } catch (InterruptedException e) {
                      return;
                  } catch (Exception e) {
                      return;
                  }
                  seekBar.setProgress(currentPosition);
              }
          }
      }).start();
      }
  
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      // TODO Auto-generated method stub
      if (fromUser) {
          Log.e("onProgressChanged: ", Integer.toString(progress));
          mPlayer.seekTo(progress);
      }
  
      }
  
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
      // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
      // TODO Auto-generated method stub
  
      }
  
  }
  </code></pre>
  
  <p>The same but throughtout AsyncTask (but the same result - still jumping):</p>
  
  <pre><code>@Override
      public void onPrepared(MediaPlayer mp) {
      seekBar.setMax(mp.getDuration());
      new Async().execute();
      }
  
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      // TODO Auto-generated method stub
      if (fromUser) {
          Log.e("onProgressChanged: ", Integer.toString(progress));
          mPlayer.seekTo(progress);
          seekBar.setProgress(progress);
          Log.e("SeekedProgressTime: ", Integer.toString(mPlayer.getCurrentPosition()));
          Log.e("SeekedProgressTime: ", Integer.toString(seekBar.getProgress()));
      }
  
      }
  
      class Async extends AsyncTask&lt;Void, Integer, Void&gt; {
  
      @Override
      protected Void doInBackground(Void... params) {
          // TODO Auto-generated method stub
          int currentPosition = 0;
          int total = mPlayer.getDuration();
          seekBar.setMax(total);
          while (mPlayer != null &amp;&amp; currentPosition &lt; total) {
          Log.e("Current mPlayer time in Thread: ", Integer.toString(mPlayer.getCurrentPosition()));
          Log.e("Current seekBar time in Thread: ", Integer.toString(seekBar.getProgress()));
          try {
              Thread.sleep(1000);
              currentPosition = mPlayer.getCurrentPosition();
          } catch (InterruptedException e) {
          } catch (Exception e) {
          }
          seekBar.setProgress(currentPosition);
          }
          return null;
      }
      }
  </code></pre>
  
  <p>Maybe, I did something wrong inside the AsyncTask, I cannot understand what is the problem with this seekBar. Check what real position it takes when you click somewhere on the seekbar, everything correct but in one moment its position value overrides somehow.</p>