Below is simple code snippet. I have android tablet and one external USB camera. Objective is to record the video from all 3 camera (at a time one, mainly external USB). Code is working on preview only but crashing on front camera and usb camera while saving. There is no clue on what is happening and no information is provided in logcat.
private void startRecordingW() {
Toast.makeText(context, "Recording Started !", Toast.LENGTH_SHORT).show();
try {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoFrameRate(30);
outputFile = new File(VIDEO_FOLDER, "video_chunk_" + System.currentTimeMillis() + ".mp4");
mediaRecorder.setOutputFile(outputFile.getAbsolutePath());
SurfaceTexture texture = textureView.getSurfaceTexture();
if (texture == null) {
throw new IllegalStateException("SurfaceTexture is not ready");
}
Surface recorderSurface = new Surface(texture);
mediaRecorder.setPreviewDisplay(recorderSurface);
mediaRecorder.setVideoSize(previewSize.getWidth(), previewSize.getHeight());
// mediaRecorder.setCaptureRate(30);
mediaRecorder.prepare();
try {
mediaRecorder.start();
} catch (IllegalStateException ex) {
Log.d(TAG, "startRecording: " + ex.getMessage());
}
isRecording = true;
new Thread(() -> {
try {
//Thread.sleep(300000); // 5 minutes
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopRecording();
startRecording(); // Start a new recording chunk
}).start();
} catch (IllegalStateException ex) {
ex.printStackTrace();
stopRecording();
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
stopRecording();
} catch (UnknownError ex) {
ex.printStackTrace();
stopRecording();
} catch (IOException e) {
e.printStackTrace();
stopRecording();
}
}
I tried all combinations, including checking camera capabilities etc. Any help or pointer would be helpful. Entire code is copied from the sample –
https://github.com/kishorpise/camtest
I am expecting it should generate files of every 30 seconds (or any time) from surface along with time stamp. Looks like mediaRecorder is having issues.
KlickIt KlickIt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.