title: SurfaceView goes black -- MediaPlayer Android
body: <p>I am using MediaPlayer and SurfaceView to stream a video from a server. The video plays fine however if the activity goes in "Paused" state, eg. when the user taps home button or recent button, On resuming the player activity the SurfaceView becomes black. I know when you leave the activity, the surfaceView is destroyed and upon resuming it is created again, so as workaround I saved the player's current position in onPause() of the activity and when the activity resumes, I seek the player to that position. This didn't work either.</p>
  
  <p><strong>So my question is how can I make the surfaceView keep the frame/picture it was displaying when the user goes out of the activity?</strong></p>
  
  <pre><code>public class VideoPlayer extends Activity implements SurfaceHolder.Callback, OnPreparedListener {
  
  
      private long PREV_PLAYER_POS;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_videoplayer);
  
          mDecorView = getWindow().getDecorView();
          mediaController = new VideoControllerView(this, mDecorView);
  
          handler = new Handler();
          Intent intent = getIntent();
          usedURL = intent.getExtras().getString("vidURL");
  
          vidID = intent.getExtras().getString("id");
          mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
          mProgressBar.setVisibility(ProgressBar.VISIBLE);
  
          mContext = this;
  
          surfView = (SurfaceView) findViewById(R.id.videoView4);
  
          SurfaceHolder holder = surfView.getHolder();
          holder.addCallback(this);
  
          mMediaPlayer = new MediaPlayer();
  
          mMediaPlayerControl = new VideoControllerView.MediaPlayerControl() {
              ……
             }
  
  
          mMediaPlayer.setOnPreparedListener(this);
  
  
          mediaController.setOnSeekStartListener(new onSeekStartListener() {
  
              @Override
              public void onSeekStarted() {
  
              }
          });
  
          mediaController.setMediaPlayer(mMediaPlayerControl);
          mediaController.setAnchorView((ViewGroup) findViewById(R.id.container4));
          mediaController.setEnabled(true);
          mediaController.show();
  
  
      }
  
  
      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                 int height) {
  
          mMediaPlayer.setDisplay(holder);
  
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
  
          mMediaPlayer.setDisplay(holder);
  
  
          try {
  
              mMediaPlayer.setDataSource(usedURL);
  
              mMediaPlayer.prepareAsync();
  
          } catch (IllegalArgumentException e) {
  
              e.printStackTrace();
          } catch (SecurityException e) {
  
              e.printStackTrace();
          } catch (IllegalStateException e) {
  
              e.printStackTrace();
          } catch (IOException e) {
  
              e.printStackTrace();
          }
  
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
  
      }
  
      @Override
      public void onPrepared(MediaPlayer mp) {
  
  
          surfView.requestLayout();
          surfView.invalidate();
  
          mMediaPlayer.start();
  
  
      }
  
  
      @Override
      public void onSurfaceViewSizeChanged(int width, int height) {
  
  
      }
  
      @Override
      protected void onResume() {
          super.onResume();
  
          mMediaPlayer.seekTo((int)PREV_PLAYER_POS);
  
  
      }
  
      @Override
      public void onBackPressed() {
          super.onBackPressed();
  
          mMediaPlayer.stop();
      }
  
      @Override
      protected void onPause() {
          super.onPause();
  
  
          PREV_PLAYER_POS = mMediaPlayer.getCurrentPosition();
  
          mMediaPlayer.pause();
  
      }
  
  
  
  
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
  
          mMediaPlayer.release();
          mMediaPlayer = null;
      }
  
  }
  </code></pre>
  
  <p>Best Regards</p>