I would like to create a video recorder and so far haven't figured out how to set parameters in order to successfully go through MediaRecorder.prepare() method.
Executing the following method
public void start() throws IOException{
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(Environment.MEDIA_MOUNTED))
{
throw new IOException("SD card is not mounted. It is " + state + ".");
}
File directory = new File(path).getParentFile();
if(!directory.exists() && !directory.mkdirs())
{
throw new IOException("Path to file could not be created.");
}
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
recorder.setVideoFrameRate(15);
recorder.setVideoSize(176, 144);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
this.state = VideoRecorderState.STATE_RECORDING;
}
it throws an exception on line recorder.prepare().
How to set parameters in order to be able to capture video?
Maybe the Camera application's source helps you debug this.
I am exactly answering this question in the following tutorial: http://integratingstuff.wordpress.com/2010/10/18/writing-code-that-captures-videos-on-android/
The reason why your code is failing on prepare() is because you didn't set all the necessary properties. For example, you also need to set maxDuration.
This could be a permissions error. Do you have the android.permission.CAMERA permission set in your AndroidManifest file?
Have you checked this out?
http://code.google.com/p/android/issues/detail?id=5050
These guys suggest that it is a timing issue, and that the MediaRecorder state machine may require some delay (hardware dependent?) between states.
It would be nice if there were callbacks for when each state was fully achieved - then we could just put prepare in that.
I had the same question. I was going from a background audio recording service, and hoping to create a background video recording service. You can't truly record background video, but you can make the video preview very small in your existing UI. I followed the tutorial: http://integratingstuff.wordpress.com/2010/10/18/writing-code-that-captures-videos-on-android/ and the sample Camera Preview demo. But ultimately the sample code in http://www.apress.com/downloadable/download/sample/sample_id/39/ was simple enough to tweek, but also complete enough to work with setCamera. I will post my solution here to save others time in their progression from toy examples, to a complex example with good quality background video recording (using front facing camera if necessary).
This is the source for an Android video recorder with "no" preview (the preview is a 1x1 pixel which simulates an unobtrusive recording led), to record video without distracting users. To use your own UI, simply change the video_recorder.xml to your layout (be sure to keep the VideoView). It was tested on Android 2.2 and 3.0 devices.
Suitable use cases:
Layout xml:
Java class:
Here is a snippet that works:
THE most important thing is the surface. You don't have it, so without it it fails.
Regards
BeMeCollective