我正在为我的应用程序创建录像机功能。我使用以下代码准备相机和媒体记录器api:
private boolean prepareCameraRecorder() {
// BEGIN_INCLUDE (configure_preview)
int numCameras = Camera.getNumberOfCameras();
int camId = 0;
if (numCameras == 1) {
PackageManager pm = getPackageManager();
boolean frontCam = pm
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
boolean rearCam = pm
.hasSystemFeature(PackageManager.FEATURE_CAMERA);
if (frontCam) {
camera = CameraHelper.getDefaultFrontFacingCameraInstance();
camId = Camera.CameraInfo.CAMERA_FACING_BACK;
} else if (rearCam) {
camera = CameraHelper.getDefaultBackFacingCameraInstance();
camId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
} else if (numCameras == 2) {
if (CAMERA_TYPE == CAMERA_FRONT) {
Utils.getInstance().printDebug("displaying front camera ");
camera = CameraHelper.getDefaultFrontFacingCameraInstance();
camId = Camera.CameraInfo.CAMERA_FACING_FRONT;
} else if (CAMERA_TYPE == CAMERA_BACK) {
Utils.getInstance().printDebug("displaying back camera ");
camera = CameraHelper.getDefaultBackFacingCameraInstance();
camId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
}
// 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.
parameters = camera.getParameters();
List<Camera.Size> mSupportedPreviewSizes = parameters
.getSupportedPreviewSizes();
Camera.Size optimalSize = CameraHelper.getOptimalPreviewSize(
mSupportedPreviewSizes, cameraView.getWidth(),
cameraView.getHeight());
// Use the same size for recording profile.
// Check profile in tab:
int profile = -1;
// set camera profile
if (CamcorderProfile.hasProfile(camId, CamcorderProfile.QUALITY_HIGH)) {
profile = CamcorderProfile.QUALITY_HIGH;
} else if (CamcorderProfile.hasProfile(camId,
CamcorderProfile.QUALITY_480P)) {
profile = CamcorderProfile.QUALITY_480P;
} else if (CamcorderProfile.hasProfile(camId,
CamcorderProfile.QUALITY_720P)) {
profile = CamcorderProfile.QUALITY_720P;
} else if (CamcorderProfile.hasProfile(camId,
CamcorderProfile.QUALITY_1080P)) {
profile = CamcorderProfile.QUALITY_1080P;
} else if (CamcorderProfile.hasProfile(camId,
CamcorderProfile.QUALITY_CIF)) {
profile = CamcorderProfile.QUALITY_CIF;
} else if (CamcorderProfile.hasProfile(camId,
CamcorderProfile.QUALITY_LOW)) {
profile = CamcorderProfile.QUALITY_LOW;
} else if (CamcorderProfile.hasProfile(camId,
CamcorderProfile.QUALITY_QCIF)) {
profile = CamcorderProfile.QUALITY_QCIF;
} else if (CamcorderProfile.hasProfile(camId,
CamcorderProfile.QUALITY_QVGA)) {
profile = CamcorderProfile.QUALITY_QVGA;
}
//
CamcorderProfile camProfile = null;
camProfile = CamcorderProfile.get(camId, profile);
if (profile != -1) {
Utils.getInstance().printDebug("profile: " + profile);
camProfile.videoFrameWidth = optimalSize.width;
camProfile.videoFrameHeight = optimalSize.height;
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
camera.setParameters(parameters);
parameters = camera.getParameters();
}
// else {
// parameters.setPreviewSize(100, 100);
// camera.setParameters(parameters);
// }
try {
camera.stopPreview();
camera.setPreviewCallback(null);
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
camera.setDisplayOrientation(90);
} catch (Exception e) {
Utils.getInstance().printDebug(
"Error starting camera preview: " + e.getMessage());
}
// BEGIN_INCLUDE (configure_media_recorder)
mediaRecorder = new MediaRecorder();
mediaRecorder.reset();
// Step 1: Unlock and set camera to MediaRecorder
camera.unlock();
mediaRecorder.setCamera(camera);
// Step 2: Set sources
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
if (profile != -1) {
mediaRecorder.setProfile(camProfile);
}
try {
// Step 4: Set output file
selFilePath = CameraHelper.getOutputMediaFile(
CameraHelper.MEDIA_TYPE_VIDEO).toString();
mediaRecorder.setOutputFile(selFilePath);
Utils.getInstance().printDebug("selFilePath: " + selFilePath);
// Step 5: Prepare configured MediaRecorder
// if (profile == -1) {
// mediaRecorder.setVideoFrameRate(30);
// }
mediaRecorder.setOrientationHint(90);
//mediaRecorder.setVideoSize(optimalSize.width, optimalSize.height);
//mediaRecorder.setVideoFrameRate(10);
// mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
// mediaRecorder.setAudioEncodingBitRate(192000);
// mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
// mediaRecorder.setVideoEncodingBitRate(12000000);
// mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG,
"IllegalStateException preparing MediaRecorder: "
+ e.getMessage());
e.printStackTrace();
releaseMediaRecorder();
return false;
} catch (Exception e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
private void startVideo(){
mediaRecorder.start()
}
如果我在手机(移动设备)中运行它,此代码工作正常但在Nexus 7(平板电脑)上失败注意:nexus 7只有前置摄像头可用。
这是错误并且发生了日志:
01-18 19:31:30.597:E / MediaRecorder(25847):开始失败:-19
01-18 19:31:31.647:E / AndroidRuntime(25847):致命异议:主要
01-18 19:31:31.647:E / AndroidRuntime(25847):处理:com.appsplanet.beasting366,PID:25847
01-18 19:31:31.647:E / AndroidRuntime(25847):java.lang.RuntimeException:start failed。
01-18 19:31:31.647:E / AndroidRuntime(25847):在android.media.MediaRecorder.start(原生方法)
01-18 19:31:31.647:E / AndroidRuntime(25847):at com.appsplanet.beasting366.ActivityVideoRecord.startVideoRecord(ActivityVideoRecord.java:633)
01-18 19:31:31.647:E / AndroidRuntime(25847):at com.appsplanet.beasting366.ActivityVideoRecord.access $ 6(ActivityVideoRecord.java:620)
01-18 19:31:31.647:E / AndroidRuntime(25847):at com.appsplanet.beasting366.ActivityVideoRecord $ 2.onClick(ActivityVideoRecord.java:581)
01-18 19:31:31.647:E / AndroidRuntime(25847):在android.view.View.performClick(View.java:4438)
01-18 19:31:31.647:E / AndroidRuntime(25847):在android.view.View $ PerformClick.run(View.java:18422)
01-18 19:31:31.647:E / AndroidRuntime(25847):在android.os.Handler.handleCallback(Handler.java:733)
01-18 19:31:31.647:E / AndroidRuntime(25847):在android.os.Handler.dispatchMessage(Handler.java:95)
01-18 19:31:31.647:E / AndroidRuntime(25847):在android.os.Looper.loop(Looper.java:136)
01-18 19:31:31.647:E / AndroidRuntime(25847):在android.app.ActivityThread.main(ActivityThread.java:5017)
01-18 19:31:31.647:E / AndroidRuntime(25847):at java.lang.reflect.Method.invokeNative(Native Method)
01-18 19:31:31.647:E / AndroidRuntime(25847):at java.lang.reflect.Method.invoke(Method.java:515)
01-18 19:31:31.647:E / AndroidRuntime(25847):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
01-18 19:31:31.647:E / AndroidRuntime(25847):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-18 19:31:31.647:E / AndroidRuntime(25847):at dalvik.system.NativeStart.main(Native Method)
有人可以帮我解决这个问题吗?
由于 Ishan jain
答案 0 :(得分:2)
不幸的是,Android Camera API并不容易理解。有两个 int constants:
public final static int Camera.CameraInfo.CAMERA_FACING_BACK = 0;
public final static int Camera.CameraInfo.CAMERA_FACING_FRONT = 1;
并且有一些方法,例如期望int cameraId
参数的Camera.open()。
但是对Camera.open()或CamcorderProfile.get()使用上面的常量是错误的。第一台相机 cameraId 0 ,第二台 1 ,第二台 2 ,依此类推到Camera.getNumberOfCameras()-1
。您可以使用以下 cameraId 检查相机的方向:
Camera camera = Camera.open(cameraId);
int facing = camera.getCameraInfo().facing();
面对的价值保证为Camera.CameraInfo.CAMERA_FACING_BACK或Camera.CameraInfo.CAMERA_FACING_FRONT。
具体来说,在 Nexus 7 上只有一台相机,因此您始终使用cameraId == 0
。
答案 1 :(得分:0)
你确定你的病情正确吗?
if (frontCam) {
camera = CameraHelper.getDefaultFrontFacingCameraInstance();
camId = Camera.CameraInfo.CAMERA_FACING_BACK;
} else if (rearCam) {
camera = CameraHelper.getDefaultBackFacingCameraInstance();
camId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
如果设备只有前凸轮而不是后背。