我正在使用 MediaRecorder 录制视频。当第三方屏幕录像机运行时媒体录像机通过非法状态异常。如果我杀死屏幕录像机应用程序,一切都会按预期工作。即使另一个应用程序已经在使用它,是否有一个优雅的解决方案允许我的应用程序访问 MediaRecorder?
问问题
1144 次
1 回答
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;
}
于 2018-01-02T06:41:22.130 回答