【发布时间】:2016-06-15 14:53:48
【问题描述】:
我有以下方法来启动相机预览并准备 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
我需要设置一些其他参数吗?
【问题讨论】:
标签: android android-camera mediarecorder