我已经在三星平板电脑上测试了我的代码并注意,但相同的代码在 nexus 上不起作用。
我知道nexus只有前置摄像头,并且摄像头正在打开,但是单击开始录制按钮它会崩溃....我通过setProfile上的错误检查了它。
我的代码:
public boolean prepareVideoRecorder() {
Toast.makeText(context, "Please wait..", Toast.LENGTH_SHORT).show();
// mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.stopPreview();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
int layout = getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
if (layout == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getOrientation()) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: {
// Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
int degrees = 0;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
// Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE 1111", Toast.LENGTH_SHORT).show();
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
// Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE 222-- "+ result, Toast.LENGTH_SHORT).show();
}
mMediaRecorder.setOrientationHint(result);
break;
}
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: {
// Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
int degrees = 270;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (CurrentValues.cur_cam_face == 1) {
// Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT 111", Toast.LENGTH_SHORT).show();
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
// System.out.println("FRONT CAMERA )))))))");
} else { // back-facing
// Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT 222", Toast.LENGTH_SHORT).show();
result = degrees;// (info.orientation - degrees + 360) % 360;
// System.out.println("BACK CAMERA )))))))");
}
mMediaRecorder.setOrientationHint(result);
break;
}
case Configuration.ORIENTATION_LANDSCAPE: {
// Toast.makeText(context, "ORIENTATION_LANDSCAPE", Toast.LENGTH_LONG).show();
int degrees = 180;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
// Toast.makeText(context, "ORIENTATION_LANDSCAPE 111", Toast.LENGTH_SHORT).show();
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
// Toast.makeText(context, "ORIENTATION_LANDSCAPE 22", Toast.LENGTH_SHORT).show();
result = (info.orientation - degrees + 360) % 360;
}
mMediaRecorder.setOrientationHint(result);
break;
}
default: {
//Toast.makeText(context, "DEFAULT 11", Toast.LENGTH_SHORT).show();
int degrees = 90;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (CurrentValues.cur_cam_face == 1) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = degrees;// (info.orientation - degrees + 360) % 360;
}
mMediaRecorder.setOrientationHint(result);
}
}
}else if (layout == Configuration.SCREENLAYOUT_SIZE_LARGE) {
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getOrientation()) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: {
Toast.makeText(context, "PORTRAIT " + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, Toast.LENGTH_SHORT).show();
int degrees = 0;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
Toast.makeText(context, "SCREEN_PORTRAIT 1111", Toast.LENGTH_SHORT).show();
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
Toast.makeText(context, "SCREEN_PORTRAIT 222-- "+ result, Toast.LENGTH_SHORT).show();
}
mMediaRecorder.setOrientationHint(result);
break;
}
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: {
Toast.makeText(context, "else LANDSCAPE " +ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, Toast.LENGTH_SHORT).show();
int degrees = 90;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (CurrentValues.cur_cam_face == 1) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = degrees;// (info.orientation - degrees + 360) % 360;
}
mMediaRecorder.setOrientationHint(result);
break;
}
}
}else
{
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getOrientation()) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: {
Toast.makeText(context, "PORTRAIT " + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, Toast.LENGTH_SHORT).show();
int degrees = 0;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
Toast.makeText(context, "SCREEN_PORTRAIT 1111", Toast.LENGTH_SHORT).show();
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
Toast.makeText(context, "SCREEN_PORTRAIT 222-- "+ result, Toast.LENGTH_SHORT).show();
}
mMediaRecorder.setOrientationHint(result);
break;
}
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: {
Toast.makeText(context, "else LANDSCAPE " +ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, Toast.LENGTH_SHORT).show();
int degrees = 90;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
int result;
if (CurrentValues.cur_cam_face == 1) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = degrees;// (info.orientation - degrees + 360) % 360;
}
mMediaRecorder.setOrientationHint(result);
break;
}
}
}
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
CamcorderProfile camcorderProfile_HQ;
if (CurrentValues.cur_cam_face == 1)
camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
else
camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mMediaRecorder.setProfile(camcorderProfile_HQ);
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO)
.toString());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: 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;
}