I m using MediaRecorder to record video. while third-party screen recorder is running at time media recorder through illegal state exception. Everything works as expected if I kill the screen recorder app. Is there an elegant solution to allow my app to access the MediaRecorder even if another app is already using it?
Only one app at a time can access the microphone. while screen recorder app is running and can access Microphone at the time I have tried to record video using inbuild native camera but can't record video due to microphone busy with another app.
Also, there is no standard way to inform another app that you want access to the microphone (so that they release the resources and let you access it).I would recommend you to simply inform the user that the microphone is currently not available because you can't do anything else or we can record video without audio.
Using below code, I have recorded video without audio.
private boolean prepareVideoRecorder() {
mCamera = Camera.open(findBackFacingCamera());
mediaRecorder = new MediaRecorder();
// store the quality profile required
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
CamcorderProfile profile;
if (windowwidth > 2000) {
profile= CamcorderProfile.get(camid, CamcorderProfile.QUALITY_HIGH);
} else {
profile= CamcorderProfile.get(camid, CamcorderProfile.QUALITY_480P);
}
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
// Step 2: Set sources value i.e. CAMERA,SURFACE, or DEFAULT
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set all other values contained in profile except audio settings
mediaRecorder.setOutputFormat(profile.fileFormat);
mediaRecorder.setVideoEncoder(profile.videoCodec);
mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mediaRecorder.setVideoFrameRate(profile.videoFrameRate);
mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
// Step 4: Seting output file
// mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
File videoStorageDir = Const.getFileDirectory(MainActivity.this);
mediaRecorder.setOutputFile(videoStorageDir.getPath()+ File.separator + "/videofile.mp4");
// Step 5: Set the preview output
mediaRecorder.setPreviewDisplay(mvPreview.getHolder().getSurface());
mediaRecorder.setMaxDuration(42000); // for 40 second (you can set video recording limit here)
// Step 6: Prepare configured MediaRecorder
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}