title: play video immediately after recording on android app
body: <p>I am developing an app which should record a video of Maximum limit one minute, and saving it in gallery and then right after it saves it should start playing automatically. 
  Following is my code.</p>
  
  <pre><code>public class MainActivity extends Activity  {
  
  private Camera myCamera;
    private MyCameraSurfaceView myCameraSurfaceView;
    private MediaRecorder mediaRecorder;
  
  Button myButton;
  SurfaceHolder surfaceHolder;
  boolean recording;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  
        recording = false;
  
        setContentView(R.layout.main);
  
        //Get Camera for preview
        myCamera = getCameraInstance();
        if(myCamera == null){
         Toast.makeText(MainActivity.this,
           "Fail to get Camera",
           Toast.LENGTH_LONG).show();
        }
  
        myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
        FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
        myCameraPreview.addView(myCameraSurfaceView);
  
        myButton = (Button)findViewById(R.id.mybutton);
        myButton.setOnClickListener(myButtonOnClickListener);
        Log.i("Camera !Error", "Cameray ka koi masla ni Agay chcek karo");
    }
  
    Button.OnClickListener myButtonOnClickListener
    = new Button.OnClickListener(){
  
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   if(recording){
                // stop recording and release camera
                mediaRecorder.stop();  // stop the recording
                releaseMediaRecorder(); // release the MediaRecorder object
  
                //Exit after saved
                finish();
   }else{
  
    //Release Camera before MediaRecorder start
    releaseCamera();
  
          if(!prepareMediaRecorder()){
           Toast.makeText(MainActivity.this,
             "Fail in prepareMediaRecorder()!\n - Ended -",
             Toast.LENGTH_LONG).show();
           finish();
          }
  
    mediaRecorder.start();
    recording = true;
    myButton.setText("STOP");
    Log.i("Camera !Error", "Record to stop");
   }
  }};
  
    private Camera getCameraInstance(){
  // TODO Auto-generated method stub
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            Log.i("Camera Error", "Camera Not Found");
        }
        Log.i("Camera Error", "Camera Found");
        return c; // returns null if camera is unavailable
  }
  
  private boolean prepareMediaRecorder(){
     myCamera = getCameraInstance();
     mediaRecorder = new MediaRecorder();
  
     myCamera.unlock();
     mediaRecorder.setCamera(myCamera);
  
     mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
     mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  
     mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
  
     mediaRecorder.setOutputFile("/sdcard/myvideo2.3gp");
        mediaRecorder.setMaxDuration(10000); // Set max duration 10 sec.
        mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
  
     mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
     Log.i("Camera Error", "Media Recorder is ready");
  
     try {
         mediaRecorder.prepare();
     } catch (IllegalStateException e) {
         releaseMediaRecorder();
         return false;
     } catch (IOException e) {
         releaseMediaRecorder();
         return false;
     }
     return true;
  
  }
  
    @Override
    protected void onPause() {
        super.onPause();
        releaseMediaRecorder();       // if you are using MediaRecorder, release it first
        releaseCamera();              // release the camera immediately on pause event
    }
  
    private void releaseMediaRecorder(){
        if (mediaRecorder != null) {
            mediaRecorder.reset();   // clear recorder configuration
            mediaRecorder.release(); // release the recorder object
            mediaRecorder = null;
            myCamera.lock();           // lock camera for later use
        }
        Log.i("Media Recorder", "Media Recorer Released");
  
    }
  
    private void releaseCamera(){
        if (myCamera != null){
            myCamera.release();        // release the camera for other applications
            myCamera = null;
        }
        Log.i("Camera Recorder", "Camera Released");
    }
  
  public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
  
  private SurfaceHolder mHolder;
     private Camera mCamera;
  
  public MyCameraSurfaceView(Context context, Camera camera) {
         super(context);
         mCamera = camera;
  
         // Install a SurfaceHolder.Callback so we get notified when the
         // underlying surface is created and destroyed.
         mHolder = getHolder();
         mHolder.addCallback(this);
         // deprecated setting, but required on Android versions prior to 3.0
         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
     }
  
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int weight,
    int height) {
         // If your preview can change or rotate, take care of those events here.
         // Make sure to stop the preview before resizing or reformatting it.
  
         if (mHolder.getSurface() == null){
           // preview surface does not exist
           return;
         }
  
         // stop preview before making changes
         try {
             mCamera.stopPreview();
         } catch (Exception e){
           // ignore: tried to stop a non-existent preview
         }
  
         // make any resize, rotate or reformatting changes here
  
         // start preview with new settings
         try {
             mCamera.setPreviewDisplay(mHolder);
             mCamera.startPreview();
  
         } catch (Exception e){
         }
  }
  
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
   // TODO Auto-generated method stub
   // The Surface has been created, now tell the camera where to draw the preview.
         try {
             mCamera.setPreviewDisplay(holder);
             mCamera.startPreview();
         } catch (IOException e) {
         }
  }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
   // TODO Auto-generated method stub
  
  }
  }
  }
  </code></pre>
  
  <p>my main.xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"&gt;
  &lt;FrameLayout
  android:id="@+id/videoview"
  android:layout_width="720px"
  android:layout_height="480px"/&gt;
  &lt;Button
  android:id="@+id/mybutton"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="REC"
  android:textSize="12sp"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>On button click recording starts and on click again, it stops and exits using 
  finish() for this purpose. Here i want that instead of exiting the recorded video should start.
  Hope i managed to clearly explain my question.</p>