I am working on a project named as Anti-Theft android app. There are two tasks one is to capture intruder picture and the second is to capture intruder video for 5 to 10 seconds (on 3 wrong password attempts). The take picture method is working fine, but the takeVideo() method is not working for me. It says Error: -22.
Android Version: 9 Target SDK 33 Min SDK 24
Calling takeVideo() method from onPasswordFailed method.
Code:
void takeVideo(int seconds) {
backgroundHandler.post(new Runnable() {
@Override
public void run() {
camera = null;
mediaRecorder = null;
SurfaceTexture surfaceTexture = new SurfaceTexture(10);
try {
camera = Camera.open(0);
mediaRecorder = new MediaRecorder();
camera.setPreviewTexture(surfaceTexture);
camera.startPreview();
// Set the audio source (e.g., MIC) before setting the audio encoder
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// Set the video source and other media recorder settings
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
// mediaRecorder.setMaxDuration(10 * 1000);
// Set the audio encoder
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
// Set the output file path for the video
String fileName = "video_" + System.currentTimeMillis() + ".mp4";
String directoryName = "AtexAntiTheft";
File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), directoryName);
if (!directory.exists()) {
directory.mkdirs(); // Create the directory if it doesn't exist
}
File videoFile = new File(directory, fileName);
// Set the output file path for the video
mediaRecorder.setOutputFile(videoFile.getPath());
// Prepare and start recording with some delay (e.g., 1000ms)
try {
mediaRecorder.prepare();
Thread.sleep(1000); // Add a delay to allow initialization
mediaRecorder.start();
// Record video for the specified duration
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
Log.e("Debug 1", "Error: "+e.getMessage());
e.printStackTrace();
} finally {
// Stop and release media recorder
if (mediaRecorder != null) {
try {
mediaRecorder.stop();
} catch (Exception e) {
Log.e("Debug 2","Error: "+ e.getMessage());
e.printStackTrace();
}
mediaRecorder.reset();
mediaRecorder.release();
}
// Release the camera after recording the video successfully
if (camera != null) {
camera.stopPreview();
camera.release();
}
// Invoke the callback with the video file path
if (videoTakenCallback != null) {
videoTakenCallback.onVideoTaken(videoFile.getPath());
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("Debug 3", "Error: "+e.getMessage());
// Handle any exceptions and release resources
if (camera != null) {
camera.release();
}
if (mediaRecorder != null) {
mediaRecorder.reset();
mediaRecorder.release();
}
}
}
});
}
I have tried searching for the solutions, but nothing worked for me. and I am expecting to capture the intruder's short videos from lock screen.