title: Asynchronous Video Streaming Android
body: <p>I have been working on video playback of progressive videos from my android application. I am able to stream videos from internet.</p>
  
  <p>But the video starts after buffering the whole data.</p>
  
  <p>Here is the code I am using-</p>
  
  <pre><code>private static final String MOVIEURL = "movieUrl";
  private static final String TAG = "VideoPlayerActivity";
  private int mVideoWidth;
  private int mVideoHeight;
  private MediaPlayer mMediaPlayer;
  private SurfaceView mPreview;
  private SurfaceHolder holder;
  private String path;
  private Bundle extras;
  private boolean mIsVideoSizeKnown = false;
  private boolean mIsVideoReadyToBePlayed = false;    
  
  protected void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.mediaplayer_2);
      mPreview = (SurfaceView) findViewById(R.id.surface);
      holder = mPreview.getHolder();
      holder.addCallback(this);
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      extras = getIntent().getExtras();
  }
  
   private void playVideo(String movieUrl) {
          doCleanUp();
          try {
              path=movieUrl;
  
  
              // Create a new media player and set the listeners
              mMediaPlayer = new MediaPlayer();
              mMediaPlayer.setDataSource(path);
              mMediaPlayer.setDisplay(holder);
              mMediaPlayer.prepare();
              mMediaPlayer.setOnBufferingUpdateListener(this);
              mMediaPlayer.setOnCompletionListener(this);
              mMediaPlayer.setOnPreparedListener(this);
              mMediaPlayer.setOnVideoSizeChangedListener(this);
              mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  
  
          } catch (Exception e) {
              Log.e(TAG, "error: " + e.getMessage(), e);
              return false;
          }
      }
  
     public void onBufferingUpdate(MediaPlayer arg0, int percent) {
          Log.d(TAG, "onBufferingUpdate percent:" + percent);
  
      }
  
      public void onCompletion(MediaPlayer arg0) {
          Log.d(TAG, "onCompletion called");
      }
  
  
    private void doCleanUp() {
          mVideoWidth = 0;
          mVideoHeight = 0;
          mIsVideoReadyToBePlayed = false;
          mIsVideoSizeKnown = false;
      }
  
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
          Log.v(TAG, "onVideoSizeChanged called");
          if (width == 0 || height == 0) {
              Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
              return;
          }
          mIsVideoSizeKnown = true;
          mVideoWidth = width;
          mVideoHeight = height;
          if (mIsVideoReadyToBePlayed &amp;&amp; mIsVideoSizeKnown) {
              startVideoPlayback();
          }
      }
  
      public void onPrepared(MediaPlayer mediaplayer) {
          Log.d(TAG, "onPrepared called");
          mIsVideoReadyToBePlayed = true;
          if (mIsVideoReadyToBePlayed &amp;&amp; mIsVideoSizeKnown) {
              startVideoPlayback();
          }
      }
  
      public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
          Log.d(TAG, "surfaceChanged called");
  
      }
  
      public void surfaceDestroyed(SurfaceHolder surfaceholder) {
          Log.d(TAG, "surfaceDestroyed called");
      }
  
  
      public void surfaceCreated(SurfaceHolder holder) {
          Log.d(TAG, "surfaceCreated called");
         playVideo(extras.getString(MOVIEURL));
  
  
  
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          releaseMediaPlayer();
          doCleanUp();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          releaseMediaPlayer();
          doCleanUp();
      }
  
      private void releaseMediaPlayer() {
          if (mMediaPlayer != null) {
              mMediaPlayer.release();
              mMediaPlayer = null;
          }
      }
  
      private void startVideoPlayback() {
          Log.v(TAG, "startVideoPlayback");
          holder.setFixedSize(mVideoWidth, mVideoHeight);
          mMediaPlayer.start();
      }
  </code></pre>
  
  <p>This code is working fine, but not playing before the whole video gets buffered.</p>
  
  <p>Is there anyway we can stream the video while buffering it in the background.</p>
  
  <p>Thanks.</p>