How to capture video in Android?

2019-01-16 07:48发布

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?

7条回答
霸刀☆藐视天下
2楼-- · 2019-01-16 08:20

In my case, copying and pasting the samples above didn't work. Then, viewing the methods in MediaRecorder, I found setPreviewDisplay. I called this method passing the surface used in Camera.setPreviewDisplay, the IOException in .prepare is gone and I was able to record video. Try yourself and post your results.

查看更多
登录 后发表回答