title: Mp3 has paused when screen locked
body: <p>mp3 pauses when on screen lock..here is my main java code...I've searched some information but nothing helped me.And by the way After clicking Play, then stop, then back to play, the application force crashes, sometimes it requires more clicks, sometimes fewer..Can someone help me please..Thank you !</p>
  
  <pre><code>import android.app.Activity;
  import android.media.MediaPlayer;
  import android.media.MediaPlayer.OnBufferingUpdateListener;
  import android.media.MediaPlayer.OnPreparedListener;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ProgressBar;
  
  public class Main extends Activity implements OnClickListener {
  
  private ProgressBar playSeekBar;
  
  private Button buttonPlay;
  
  private Button buttonStopPlay;
  
  private MediaPlayer player;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      initializeUIElements();
  
      initializeMediaPlayer();
  }
  private void initializeUIElements() {
  
      playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
      playSeekBar.setMax(100);
      playSeekBar.setVisibility(View.INVISIBLE);
  
      buttonPlay = (Button) findViewById(R.id.buttonPlay);
      buttonPlay.setOnClickListener(this);
  
      buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
      buttonStopPlay.setEnabled(false);
      buttonStopPlay.setOnClickListener(this);
  
  }
  public void onClick(View v) {
      if (v == buttonPlay) {
          startPlaying();
      } else if (v == buttonStopPlay) {
          stopPlaying();
      }
  }  
  private void startPlaying() {
      buttonStopPlay.setEnabled(true);
      buttonPlay.setEnabled(false);
  
      playSeekBar.setVisibility(View.VISIBLE);
  
      player.prepareAsync();
  
      player.setOnPreparedListener(new OnPreparedListener() {
  
          public void onPrepared(MediaPlayer mp) {
              player.start();
          }
      });
  
  }
  
  private void stopPlaying() {
      if (player.isPlaying()) {
          player.stop();
          player.release();
          initializeMediaPlayer();
      }
  
      buttonPlay.setEnabled(true);
      buttonStopPlay.setEnabled(false);
      playSeekBar.setVisibility(View.INVISIBLE);
  }
  
  private void initializeMediaPlayer() {
      player = new MediaPlayer();
      try {
          player.setDataSource("http://users1.jabry.com/mine/inna.mp3");
      } catch (IllegalArgumentException e) {
          e.printStackTrace();
      } catch (IllegalStateException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
  
          public void onBufferingUpdate(MediaPlayer mp, int percent) {
              playSeekBar.setSecondaryProgress(percent);
              Log.i("Buffering", "" + percent);
          }
      });
  }
  
  @Override
  protected void onPause() {
      super.onPause();
      if (player.isPlaying()) {
          player.stop();
      }
  }
  }
  </code></pre>