title: I am trying to record a video using a service
body: <p>i am trying to develop an application which records a video for a certian time in the background and then stops automatically. I am able to initialize all the necessary things but not able to record it</p>
  
  <p>My main actiVity:</p>
  
  <pre><code> mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
  mSurfaceHolder = mSurfaceView.getHolder();
  mSurfaceHolder.addCallback(this);
  mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  Button btnStart = (Button) findViewById(R.id.button1);
  
  btnStart.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          Intent intent = new Intent(StartTestActivity.this,
                  RecorderService.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startService(intent);
          finish();
  </code></pre>
  
  <p>My RecorderService:</p>
  
  <pre><code>import android.app.Service;
  import android.content.Intent;
  import android.graphics.PixelFormat;
  import android.hardware.Camera;
  import android.hardware.Camera.Size;
  import android.media.MediaRecorder;
  import android.os.Environment;
  import android.os.IBinder;
  import android.util.Log;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.widget.Toast;
  
  import java.io.IOException;
  import java.util.List;
  
  public class RecorderService extends Service {
  private static final String TAG = "RecorderService";
  private SurfaceView mSurfaceView;
  private SurfaceHolder mSurfaceHolder;
  private static Camera mServiceCamera;
  private boolean mRecordingStatus;
  private MediaRecorder mMediaRecorder;
  
  @Override
  public void onCreate() {
      mRecordingStatus = false;
      mServiceCamera = Camera.open(1);
      mSurfaceView = MainActivity.mSurfaceView;
      mSurfaceHolder = MainActivity.mSurfaceHolder;
      super.onCreate();
  }
  
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      super.onStartCommand(intent, flags, startId);
      if (mRecordingStatus == false)
          startRecording();
      return START_STICKY;
  }
  
  @Override
  public void onDestroy() {
      stopRecording();
      mRecordingStatus = false;
  
      super.onDestroy();
  }
  
  public boolean startRecording() {
      try {
          Camera.Parameters params = mServiceCamera.getParameters();
          mServiceCamera.setParameters(params);
          Camera.Parameters p = mServiceCamera.getParameters();
  
          final List&lt;Size&gt; listSize = p.getSupportedPreviewSizes();
          Size mPreviewSize = listSize.get(2);
          Log.v(TAG, "use: width = " + mPreviewSize.width
                  + " height = " + mPreviewSize.height);
          p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
          p.setPreviewFormat(PixelFormat.YCbCr_420_SP);
          mServiceCamera.setParameters(p);
  
          try {
              mServiceCamera.setPreviewDisplay(mSurfaceHolder);
              mServiceCamera.startPreview();
          }
          catch (IOException e) {
              Log.e(TAG, e.getMessage());
              e.printStackTrace();
          }
  
          mServiceCamera.unlock();
  
          mMediaRecorder = new MediaRecorder();
          mMediaRecorder.setCamera(mServiceCamera);
          mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
          mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
          mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
          mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
  mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()+"/vid
  eo.mp4");
          mMediaRecorder.setVideoFrameRate(30);
          mMediaRecorder.setVideoSize(mPreviewSize.width, 
  mPreviewSize.height);
          mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
          mMediaRecorder.setMaxDuration(5000);
          mMediaRecorder.prepare();
          mMediaRecorder.start();
          mRecordingStatus = true;
          return true;
      } catch (IllegalStateException e) {
          Log.d(TAG, e.getMessage());
          e.printStackTrace();
          return false;
      } catch (IOException e) {
          Log.d(TAG, e.getMessage());
          e.printStackTrace();
          return false;
      }
  }
  
  public void stopRecording() {
      Toast.makeText(getBaseContext(), "Recording Stopped",     
  Toast.LENGTH_SHORT).show();
      try {
          mServiceCamera.reconnect();
      } catch (IOException e) {
          e.printStackTrace();
      }
      mMediaRecorder.stop();
      mMediaRecorder.reset();
      mServiceCamera.stopPreview();
      mMediaRecorder.release();
      mServiceCamera.release();
      mServiceCamera = null;
  }
  }
  </code></pre>
  
  <p>My errors:</p>
  
  <pre><code>08-20 11:10:54.184  12603-12603/com.example.svbhargav.mediafilesharing 
  E/MediaRecorder﹕ start failed: -19
  08-20 11:10:54.189  12603-12603/com.example.svbhargav.mediafilesharing  
  E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: com.example.svbhargav.mediafilesharing, PID: 12603
  java.lang.RuntimeException: Unable to start service 
  com.example.svbhargav.mediafilesharing.RecorderService@32ef1113 with Intent 
  { flg=0x10000000 cmp=com.example.svbhargav.mediafilesharing/.RecorderService 
  }: java.lang.RuntimeException: start failed.
          at 
  android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2942)
          at android.app.ActivityThread.access$2200(ActivityThread.java:154)
          at     
  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1419)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5291)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at 
  
   .android.internal.os.ZygoteInit$MethodAndArgsCaller.run
   (ZygoteInit.java:904)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
   Caused by: java.lang.RuntimeException: start failed.
          at android.media.MediaRecorder.start(Native Method)
          at   
   com.example.svbhargav.mediafilesharing.RecorderService.startRecording
   (RecorderService.java:114)
    com.example.svbhargav.mediafilesharing.RecorderService.onStartCommand
   (RecorderService.java:45)
          at .ActivityThread.handleServiceArgs(ActivityThread.java:2925)     
          at android.app.ActivityThread.access$2200(ActivityThread.java:154)
  </code></pre>
  
  <p>            </p>