I'm trying capture video (on lollipop) using Media Recorder, as I understand this class support capture without preview (link, #7 in the note):
It is possible to use MediaRecorder without creating a camera preview first and skip the first few steps of this process. However, since users typically prefer to see a preview before starting a recording, that process is not discussed here.
So, I build app that start capture video automatically and stops by button clicking but I get exceptions any time, here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoSize(640, 480);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); // Tried to put this line in comment
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); // Tried to put this line in comment
/*CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(camcorderProfile);*/
recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyVideo.gpp");
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
final Button btnStopCaptureVideo = (Button) findViewById(R.id.bu_StopCapture);
btnStopCaptureVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recorder.stop();
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused
Toast.makeText(MainActivity.this, "Video create successfully", Toast.LENGTH_SHORT).show();
btnStopCaptureVideo.setEnabled(false);
}
});
}
When the code is like below I got RuntimeException
(on recorder.start()
line) and one I comment VideoEncoder
and AudioEncoder
and un-comment CamcorderProfile
I got IllegalStateException
exception (on recorder.setProfile(camcorderProfile)
line).
So, the question is how can I capture Video without preview?