title: Android Media Player with seekbar always update and reset
body: <p>everybody.</p>
  
  <p>First of all, I'm a newbie. I created an activity to play a specific mp3 from raw folder. It works fine and playing it well. I implemented the Media Player as you will see in my code below. But the problem is when I change the orientation of my smartphone , the player continues playing the sound, but a new activity is opened/ updated with the same elements reseted and if I click on PLAY button, the mp3 starts playing, but the old activity stills running on back,  playing the mp3.... so, there are 2 same songs playing now...</p>
  
  <p>I don't want to update the activity or open a new one. I just want to continue playing the song where it was playing before I change the smartphone orientation.</p>
  
  <p>Here is my code:</p>
  
  <p>MP3PlayerActivity.java (main)</p>
  
  <pre><code>public class Mp3PlayerActivity extends Activity implements OnPreparedListener {
  /** Called when the activity is first created. */
  private Button btnPlay;
  private Button btnPause;
  private int current = 0;
  private boolean   running = true;
  private int duration = 0;
  private MediaPlayer mPlayer;
  private SeekBar mSeekBarPlayer;
  private TextView mMediaTime;
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.main);
  
  
  
  
  
  
      mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.goodthings);
      btnPlay = (Button) findViewById(R.id.button1);
      btnPause = (Button) findViewById(R.id.button2);
      mMediaTime = (TextView)findViewById(R.id.mediaTime);
      mSeekBarPlayer = (SeekBar)findViewById(R.id.progress_bar);
      mPlayer.setOnPreparedListener(this);
  
      mSeekBarPlayer.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
          @Override
          public void onStopTrackingTouch(SeekBar seekBar) {}
          @Override
          public void onStartTrackingTouch(SeekBar seekBar) {}
          @Override
          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
              if(fromUser){
                  mPlayer.seekTo(progress);
                  updateTime();
              }
          }
      });
  
  
  
      btnPlay.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              try {
                  mPlayer.prepare();
              } catch (IllegalStateException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              mPlayer.start();
              mSeekBarPlayer.postDelayed(onEverySecond, 1000);
          }
  
          public void onCompletion( MediaPlayer mPlayer)
          {
              mPlayer.release();
  
          }
      });
  
  
      btnPause.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              mPlayer.pause();
          }
          public void onCompletion( MediaPlayer mPlayer)
          {
              mPlayer.release();
  
          }
      });
  }
  
  private Runnable onEverySecond = new Runnable() {
      @Override
      public void run(){
          if(true == running){
              if(mSeekBarPlayer != null) {
                  mSeekBarPlayer.setProgress(mPlayer.getCurrentPosition());
              }
  
              if(mPlayer.isPlaying()) {
                  mSeekBarPlayer.postDelayed(onEverySecond, 1000);
                  updateTime();
              }
          }
      }
  };
  
  private void updateTime(){
      do {
          current = mPlayer.getCurrentPosition();
          System.out.println("duration - " + duration + " current- "
                  + current);
          int dSeconds = (int) (duration / 1000) % 60 ;
          int dMinutes = (int) ((duration / (1000*60)) % 60);
          int dHours   = (int) ((duration / (1000*60*60)) % 24);
  
          int cSeconds = (int) (current / 1000) % 60 ;
          int cMinutes = (int) ((current / (1000*60)) % 60);
          int cHours   = (int) ((current / (1000*60*60)) % 24);
  
          if(dHours == 0){
              mMediaTime.setText(String.format("%02d:%02d / %02d:%02d", cMinutes, cSeconds, dMinutes, dSeconds));
          }else{
              mMediaTime.setText(String.format("%02d:%02d:%02d / %02d:%02d:%02d", cHours, cMinutes, cSeconds, dHours, dMinutes, dSeconds));
          }
  
          try{
              Log.d("Value: ", String.valueOf((int) (current * 100 / duration)));
              if(mSeekBarPlayer.getProgress() &gt;= 100){
                  break;
              }
          }catch (Exception e) {}
      }while (mSeekBarPlayer.getProgress() &lt;= 100);
  }
  
  @Override
  public void onPrepared(MediaPlayer arg0) {
      // TODO Auto-generated method stub
      duration = mPlayer.getDuration();
      mSeekBarPlayer.setMax(duration);
      mSeekBarPlayer.postDelayed(onEverySecond, 1000);
  }
  }
  </code></pre>