title: How to prebuffer VideoView A while another VideoView B is playing - Android
body: <p>I am interested in being able to "prebuffer" VideoView <strong>A</strong> while VideoView <strong>B</strong> is playing. Is this possible?</p>
  
  <ul>
  <li>I have two instances of a VideoView. </li>
  <li>Only one VideoView will be displayed at a given time, taking up the entire screen of the phone. </li>
  <li>The purpose is to reduce the "black screen" when switching from VideoView A and VideoView B. </li>
  <li>The app plays a list of videos one after another.</li>
  </ul>
  
  <p><strong>Added Below Code on Jan 19, 2015</strong></p>
  
  <p>The code below grabs a reference to each Player's MediaPlayer. When MediaPlayer A begins rendering, MediaPlayer B will call stop(), release(), setDataSource(url), prepareAsync(). When MediaPlayer A completes, Media Player B will call start().</p>
  
  <pre><code>public class PrebufferingActivity extends Activity {
      private VideoView player1;
      private VideoView player2;
      private MediaPlayer mediaPlayer1;
      private MediaPlayer mediaPlayer2;
      public static final String URL_1 = "sample1.mp4";
      public static final String URL_2 = "sample2.mp4";
  
      public boolean FIRST_TIME = true;
  
      @SuppressLint("NewApi")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_prebuffering);
  
          player1 = (VideoView) findViewById(R.id.videoPlayer1);
          player2 = (VideoView) findViewById(R.id.videoPlayer2);
  
          player1.setOnTouchListener(new OnTouchListener(){
  
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  // TODO Auto-generated method stub
                  return false;
              }
  
          });
  
          player2.setOnTouchListener(new OnTouchListener(){
  
               @Override
               public boolean onTouch(View v, MotionEvent event) {
                  // TODO Auto-generated method stub
                  return false;
               }
          });
          player1.setOnCompletionListener(new OnCompletionListener(){
              @Override
              public void onCompletion(MediaPlayer mp)            {
                  mediaPlayer2.start();
  
                  // Toggle visibility. Player1 is completed. Hide Player1. Show Player2.
                  player2.setVisibility(View.VISIBLE);
                  player1.setVisibility(View.INVISIBLE);
              }
          });
  
          player1.setOnInfoListener(new OnInfoListener(){
              @Override
              public boolean onInfo(MediaPlayer mp, int what, int extra) 
              {
                  if(what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
                  {
                      mediaPlayer2.stop();
                      mediaPlayer2.release();
  
                      try {
                          mediaPlayer2.setDataSource(URL_2);
                          mediaPlayer2.prepareAsync();
                      } catch (Exception e) {
                          e.printStackTrace();
                      } 
                  }
                  return false;
              }
          });
  
          player1.setOnPreparedListener(new OnPreparedListener(){
              @Override
              public void onPrepared(MediaPlayer mp)          {
                  mediaPlayer1 = mp;
  
                  if(FIRST_TIME == true) {
                      mediaPlayer1.start();
                      player1.requestFocus();
                      FIRST_TIME = false;
                  }
              }
          });
  
          player2.setOnCompletionListener(new OnCompletionListener(){
  
              @Override
              public void onCompletion(MediaPlayer mp)            {
                  mediaPlayer1.start();
                  player1.requestFocus();
  
                  // Toggle visibility. Player2 is completed. Hide Player2. Show Player1.
                  player1.setVisibility(View.VISIBLE);
                  player2.setVisibility(View.INVISIBLE);
              }
          });
  
          player2.setOnInfoListener(new OnInfoListener(){
              @Override
              public boolean onInfo(MediaPlayer mp, int what, int extra) 
              {
                  if(what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
                  {
                      // Prepare Player 2
                      mediaPlayer1.stop();
                      mediaPlayer1.release();
                      try {
                          mediaPlayer1.setDataSource(PrebufferingActivity.URL_1);
                          mediaPlayer1.prepareAsync();
                      } catch (Exception e) {
                          e.printStackTrace();
                      } 
                  }
  
                  return false;
              }
          });
  
          player2.setOnPreparedListener(new OnPreparedListener(){
              @Override
              public void onPrepared(MediaPlayer mp) {
                  mediaPlayer2 = mp;
              }
          });
  
          // Player 1
          player1.setMediaController(new MediaController(this));
          player1.setVideoURI(Uri.parse(URL_1));
  
          // Player 2
          player2.setMediaController(new MediaController(this));
          player2.setVideoURI(Uri.parse(URL_2));
      }
  </code></pre>