我正在尝试开发一个可以录制用户视频的应用程序。因此,如果有前置摄像头,我需要使用前置摄像头拍摄视频。我建立了一个相机预览,这很好。我使用 Android How-To Sites 构建了 MediaRecorder 并进行了设置。如果我使用 CamcorderProfile,当我调用 start() 时,我的媒体服务器就会死机。如果我自己设置编码器,媒体服务器会在 start() 处引发运行时异常,并显示消息“启动失败:-19”,我在这里发现了一些关于这个主题的问题,但没有一个能解决我的问题。我认为这可能与我没有使用后置摄像头有关。也许我没有找到正确的纪录片来构建正确的代码。我认为这不仅仅是我的问题,我很乐意获得更多关于相机使用的知识。我的代码如下:
设置预览的 onResume()
protected void onResume() {
super.onResume();
// 1. set up camera preview
if(checkCameraHardware(this)){
mCamera = getCameraInstance();
mCameraPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(id.cameraPreview);
preview.addView(mCameraPreview);
}
else{
Log.d("Recorder", "camera check returned false");
}
}
使用的方法 checkCameraHardware()
private boolean checkCameraHardware(Context context){
boolean ret = true;
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
ret = true;
}
else {
ret = false;
}
return ret;
}
和方法 getCameraInstance()
public static Camera getCameraInstance(){
Camera c = null;
int cnum = 0;
mCamSelect = 0;
Camera.CameraInfo caminfo = new CameraInfo();
try {
cnum = Camera.getNumberOfCameras();
Log.d("getCameraInstance", String.valueOf(cnum));
for(int i = 0;i<cnum;i++){
Camera.getCameraInfo(i, caminfo);
if(caminfo.facing == CameraInfo.CAMERA_FACING_FRONT){
mCamSelect = i;
break;
}
}
c = Camera.open(mCamSelect); // attempt to get a Camera instance
}
catch (Exception e){
Log.d("getCameraInstance", "FATAL camera could not be opened");
// Camera is not available (in use or does not exist)
}
if(c==null)Log.d("getCameraInstance", "no camera returned");
return c; // returns null if camera is unavailable
}
此代码段显示错误出现的位置(在 onClick 回调中)
if(prepareVideoRecorder()){
mMediaRecorder.start(); //here the errors occure
recording = true;
//start recording
}
以及三个MediaRecorder相关方法:prepareVideoRecorder()、releaseMediaRecorder()和release Camera()
private void releaseMediaRecorder(){
if (mMediaRecorder != null) {
mMediaRecorder.reset(); // clear recorder configuration
mMediaRecorder.release(); // release the recorder object
mMediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
private boolean prepareVideoRecorder(){
//ex: mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
CamcorderProfile profile = CamcorderProfile.get(mCamSelect, CamcorderProfile.QUALITY_HIGH);
if(profile == null){Log.d(tag, "the camcorder profile instance is null");
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
}else{
mMediaRecorder.setProfile(profile);
}
// Step 4: Set output file
//ex: mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
mMediaRecorder.setOutputFile(currentVidFile.getAbsolutePath());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mSlideview.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;
}
public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) {
// TODO Auto-generated method stub
}
}