我使用MediaRecorder录制视频。而第三方屏幕录像机正在通过非法状态异常运行时间。 如果我杀了屏幕录像机应用程序,一切都按预期工作。是否有优雅的解决方案允许我的应用访问MediaRecorder,即使其他应用已经在使用它?
答案 0 :(得分:3)
一次只能有一个应用程序可以访问麦克风。 当屏幕录像机应用程序正在运行时,我可以在尝试使用内置本机摄像机录制视频时访问麦克风但由于麦克风忙于另一个应用程序而无法录制视频。
此外,没有标准方法可以通知另一个应用你想要访问麦克风(这样他们就可以释放资源并让你访问它)。我建议你只是告诉用户麦克风当前不是可用,因为你不能做任何事情,或者我们可以录制没有音频的视频。
使用下面的代码,我录制了没有音频的视频。
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;
}