title: Android: playing large video MP4 files through http
body: <p>I have 300 MB videos on my http server...I'm trying to make good simple code to play (in 3G mode) these videos remotely in my application - sd storing or local storing is not a option. Of course I started from demo source in SDK android-17 (MediaPlayerDemo_Video, VideoViewDemo...) and in 3G mode my video doesn't play (I'm testing on Samsung Galaxy Nexus). After that I made new try in next code:</p>
  
  <pre><code>/**
   * Listing 15-4: Initializing and assigning a Surface View to a Media Player
   */
  import java.io.IOException;
  
  import mobile.dariknews.R;
  import android.app.Activity;
  import android.media.AudioManager;
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.MediaController;
  import android.widget.MediaController.MediaPlayerControl;
  
  public class SurfaceViewVideoViewActivity extends Activity 
    implements SurfaceHolder.Callback {
  
    static final String TAG = "SurfaceViewVideoViewActivity";
  
    private MediaPlayer mediaPlayer;
  
    public void surfaceCreated(SurfaceHolder holder) { 
      try {
        // When the surface is created, assign it as the
        // display surface and assign and prepare a data 
        // source.
        mediaPlayer.setDisplay(holder);
        mediaPlayer.setDataSource("http://snimkitevi-bg.com/darik/1.MP4");
        //mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.prepareAsync();
      } catch (IllegalArgumentException e) {
        Log.e(TAG, "Illegal Argument Exception", e);
      } catch (IllegalStateException e) {
        Log.e(TAG, "Illegal State Exception", e);
      } catch (SecurityException e) {
        Log.e(TAG, "Security Exception", e);
      } catch (Exception e) {      
        Log.e(TAG, "IO Exception", e);
      }
    }
  
    public void surfaceDestroyed(SurfaceHolder holder) {
      mediaPlayer.release();
    }  
  
    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width, int height) { }
  
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.surfaceviewvideoviewer);
  
      // Create a new Media Player.
      mediaPlayer = new MediaPlayer();
  
      // Get a reference to the Surface View.
      final SurfaceView surfaceView =
        (SurfaceView)findViewById(R.id.surfaceView);
  
      // Configure the Surface View.
      surfaceView.setKeepScreenOn(true);
  
      // Configure the Surface Holder and register the callback.
      SurfaceHolder holder = surfaceView.getHolder();
      holder.addCallback(this);
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      holder.setFixedSize(400, 300);
  
      // Connect a play button.
      Button playButton = (Button)findViewById(R.id.buttonPlay);
      playButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          mediaPlayer.start();        
        }
      });
  
      // Connect a pause button.
      Button pauseButton = (Button)findViewById(R.id.buttonPause);
      pauseButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          mediaPlayer.pause();        
        }
      });
  
      // Add a skip button.
      Button skipButton = (Button)findViewById(R.id.buttonSkip);    
      skipButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          mediaPlayer.seekTo(mediaPlayer.getDuration()/2);
        }
      });
  
      /**
       * Listing 15-5: Controlling playback using the Media Controller
       */
      MediaController mediaController = new MediaController(this);
      mediaController.setMediaPlayer(new MediaPlayerControl() {
        public boolean canPause() {
          return true;
        }
  
        public boolean canSeekBackward() {
          return true;
        }
  
        public boolean canSeekForward() {
          return true;
        }
  
        public int getBufferPercentage() {
          return 0;
        }
  
        public int getCurrentPosition() {
          return mediaPlayer.getCurrentPosition();
        }
  
        public int getDuration() {
          return mediaPlayer.getDuration();
        }
  
        public boolean isPlaying() {
          return mediaPlayer.isPlaying();
        }
  
        public void pause() {
          mediaPlayer.pause();
        }
  
        public void seekTo(int pos) {
          mediaPlayer.seekTo(pos);
        }
  
        public void start() {
          mediaPlayer.start();
        }
      });    
    }
  }
  </code></pre>
  
  <p>after that new try in </p>
  
  <pre><code>import android.app.Activity;
  import android.app.Application;
  import android.graphics.Bitmap;
  import android.net.Uri;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.Handler;
  import android.widget.MediaController;
  import android.widget.VideoView;
  
  public class PlayerActivity extends Activity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.videoplayer);
          new LoadOutbox().execute();
  
      }
  
      /**
       * Background Async Task to Load all OUTBOX messages by making HTTP Request
       * */
      class LoadOutbox extends AsyncTask&lt;String, String, String&gt; {
          int progressBarStatus = 0;
          private Handler progressBarHandler = new Handler();
  
          /**
           * Before starting background thread Show Progress Dialog
           * */
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
  
          }
  
          /**
           * getting Outbox JSON
           * */
          protected String doInBackground(String... args) {
              // Building Parameters
              VideoView videoView = (VideoView) findViewById(R.id.videoView1);
              videoView.setKeepScreenOn(true);
              // Attach a Media Controller MediaController mediaController = new
              // MediaController( this); videoView.setMediaController(
              // mediaController);
              MediaController mediaController = new MediaController(
                      PlayerActivity.this);
  
              videoView.setMediaController(mediaController);
  
              Uri uri = Uri.parse("http://snimkitevi-bg.com/darik/1.MP4");
              videoView.setVideoURI(uri);
              videoView.start();
  
              return null;
          }
  
          /**
           * After completing background task Dismiss the progress dialog
           * **/
          protected void onPostExecute(String file_url) {
              // dismiss the dialog after getting all products
  
              // updating UI from Background Thread
              runOnUiThread(new Runnable() {
                  public void run() {
  
                  }
              });
  
          }
  
      }
  
      public static Bitmap getLocalImageBitmap(String url) {
          return MainNewsActivities.getLocalImageBitmap(url);
      }
  
  }
  </code></pre>
  
  <p>After that I played with 50 other tests with MediaPlayer and VIewVideo... All results are same - slow and freezed video playing. My need is pretty simple - I have big video files (mp4) on my website and I want to play them on my app. So I'm here - what is the right formula for playing of large remote files in android application? </p>
  
  <p>PS: Of course I tried to use GStreamer but this framework is not compatible with last android environment - I played with in 2 days...My video is OK - in wifi mode I play <a href="http://snimkitevi-bg.com/darik/1.MP4" rel="nofollow">http://snimkitevi-bg.com/darik/1.MP4</a></p>
  
  <p>So could you give me some good advases, examples, tutorials how MediaPlayer could play normally, buffered and fastly big video files in 3G internet connection?</p>