我有以下方法来启动相机预览并准备MediaRecorder
private boolean prepareVideoRecorder(){
// BEGIN_INCLUDE (configure_preview)
//mCamera = CameraHelper.getDefaultCameraInstance();
if(Utils.hasFrontCamera())
mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
else
mCamera = Camera.open();
// We need to make sure that our preview and recording video size are supported by the
// camera. Query camera to find all the sizes and choose the optimal size given the
// dimensions of our preview surface.
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
Camera.Size optimalSize = CameraHelper.getOptimalPreviewSize(mSupportedPreviewSizes,
mSurfaceView.getWidth(), mSurfaceView.getHeight());
// Use the same size for recording profile.
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
profile.audioChannels = 2;
// profile.videoFrameWidth = optimalSize.width;
// profile.videoFrameHeight = optimalSize.height;
Log.e("Resolution ",profile.videoFrameWidth + " - "+profile.videoFrameHeight);
//profile.videoBitRate = 3000000;
//profile.videoFrameRate = 24;
// likewise for the camera object itself.
parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
mCamera.setParameters(parameters);
try {
// Requires API level 11+, For backward compatibility use {@link setPreviewDisplay}
// with {@link SurfaceView}
mCamera.setPreviewDisplay(mSurfaceView.getHolder());
if (Utils.isModeCameraLandScape(getActivity())) {
mCamera.setDisplayOrientation(ORIENTATIONS_LAND.get(Utils.getRotation(context)));
Log.e("IMAGE", "preCreateCamera land "+Utils.getRotation(context));
} else {
mCamera.setDisplayOrientation(ORIENTATIONS_PORT.get(Utils.getRotation(context)));
Log.e("IMAGE", "preCreateCamera port "+Utils.getRotation(context));
}
//mCamera.setDisplayOrientation(90);
} catch (IOException e) {
Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage());
return false;
}
// END_INCLUDE (configure_preview)
// BEGIN_INCLUDE (configure_media_recorder)
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC );
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(profile);
// Step 4: Set output file
String path = getVideoFile(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO).getAbsolutePath();
currentFile = path;
mMediaRecorder.setOutputFile(path);
mMediaRecorder.setOrientationHint(270);
// END_INCLUDE (configure_media_recorder)
// Step 5: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
我不得不对此进行评论,因为显然在我测试的特定device中(我认为它发生在其他人身上)我无法看到视频预览而且我有一个MediaRecorder - “开始失败:-19”
// profile.videoFrameWidth = optimalSize.width;
// profile.videoFrameHeight = optimalSize.height;
我的问题
当我设置
时 CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
到
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
相机似乎没有开始预览,而在Log.ei上获得“E / Resolution:1280 - 720”,但是从我在device的规格上看到的,它应该只是上升到640 - 480
我是否需要设置其他一些参数?
答案 0 :(得分:0)
在设置质量之前,您必须检查相机是否支持该质量。
答案 1 :(得分:0)
在 MediaRecorder 上设置 QUALITY_HIGH 时,我在nexus 6p设备上遇到了这个问题。
我发现 setMaxDuration()方法调用的问题导致 MediaRecorder 崩溃,其值过高。