I am trying to capture video on long press.
Here is the code that initializes the media recorder.
private void initRecorder(Surface surface) {
if (camera == null) {
camera = Camera.open();
camera.unlock();
}
if (mediaRecorder == null)
mediaRecorder = new MediaRecorder();
mediaRecorder.setPreviewDisplay(surface);
mediaRecorder.setCamera(camera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
File file_image = getDirectory();
if (!file_image.exists() && !file_image.mkdirs()) {
Toast.makeText(CameraActivity.this, "Cannot Create Directory", Toast.LENGTH_SHORT).show();
return;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddhhmmss");
String date = sdf.format(new Date());
String photoFile = "GIFVideo" + date + ".mp4";
String file_name = file_image.getAbsolutePath() + "/" + photoFile;
File picFile = new File(file_name);
mediaRecorder.setOutputFile(file_name);
// No limit. Don't forget to check the space on disk.
mediaRecorder.setMaxDuration(-1);
mediaRecorder.setVideoFrameRate(15);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
// This is thrown if the previous calls are not called with the
// proper order
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
initSuccess = true;
}
This method gets called from on onSurfaceCreated method like this:
@Override
public void surfaceCreated(SurfaceHolder holder) {
//Open Camera
try {
camera = Camera.open();
} catch (RuntimeException e) {
e.printStackTrace();
}
if (!initSuccess) {
initRecorder(holder.getSurface());
}
setCameraParameters("back");
try {
//Camera Started, tell where to draw
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
And the piece of code that is giving me error is:
btnCapture.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (!recording) {
mediaRecorder.start();
Toast.makeText(CameraActivity.this, "Video Recording Started", Toast.LENGTH_SHORT).show();
recording = true;
}
return false;
}
});
Here is the error I am getting:
E/MediaRecorder: start failed: -19
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at me.iwf.PhotoPickerDemo.CameraActivity$6.onLongClick(CameraActivity.java:283)