前置摄像头录像视频失败

时间:2015-03-21 00:39:25

标签: java android

我正在尝试使用MediaRecorder在我的一个活动中录制前置摄像头的音频。部分代码如下所示。如果我打开相机,它运行良好。但是如果我打开前置摄像头,则没有运行时错误,但是我无法打开输出MP4。在相应的路径下会生成一个新的MP4文件,文件大小看起来正常,例如11.2M。但是,如果我按下打开它,它会显示“抱歉,我们无法打开文件”。

CameraService.java

的一部分
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.d("TAG", "======= service in onStartCommand");

    if (Util.checkCameraHardware(this)) {
        mCamera = Util.getCameraInstance();
        if (mCamera != null) {
            SurfaceView sv = new SurfaceView(this);

            WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(1, 1,
                    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    PixelFormat.TRANSLUCENT);


            SurfaceHolder sh = sv.getHolder();

            sv.setZOrderOnTop(true);
            sh.setFormat(PixelFormat.TRANSPARENT);


            sh.addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    Camera.Parameters params = mCamera.getParameters();
                    mCamera.setParameters(params);
                    Camera.Parameters p = mCamera.getParameters();

                    List<Camera.Size> listSize;

                    listSize = p.getSupportedPreviewSizes();
                    Camera.Size mPreviewSize = listSize.get(2);
                    Log.v("TAG", "preview width = " + mPreviewSize.width
                            + " preview height = " + mPreviewSize.height);
                    p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);

                    listSize = p.getSupportedPictureSizes();
                    Camera.Size mPictureSize = listSize.get(2);
                    Log.v("TAG", "capture width = " + mPictureSize.width
                            + " capture height = " + mPictureSize.height);
                    p.setPictureSize(mPictureSize.width, mPictureSize.height);
                    mCamera.setParameters(p);

                    try {
                        mCamera.setPreviewDisplay(holder);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mCamera.startPreview();
                    mCamera.unlock();
                    mMediaRecorder = new MediaRecorder();
                    mMediaRecorder.setCamera(mCamera);
                    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
                    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

                    mMediaRecorder.setProfile(CamcorderProfile.get(1,CamcorderProfile.QUALITY_HIGH));

                    mMediaRecorder.setOutputFile(Util.getOutputMediaFile(Util.MEDIA_TYPE_VIDEO).getPath());

                    mMediaRecorder.setPreviewDisplay(holder.getSurface());

                    try {
                        mMediaRecorder.prepare();
                    } catch (IllegalStateException e) {
                        Log.d("TAG", "====== IllegalStateException preparing MediaRecorder: " + e.getMessage());
                    } catch (IOException e) {
                        Log.d("TAG", "====== IOException preparing MediaRecorder: " + e.getMessage());
                    }
                    mMediaRecorder.start();
                    Log.d("TAG", "========= recording start");

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mMediaRecorder.stop();
                            mMediaRecorder.reset();
                            mMediaRecorder.release();
                            mMediaRecorder=null;
                            mCamera.stopPreview();
                            mCamera.release();
                            Log.d("TAG", "========== recording finished.");
                        }
                    }, 10000);
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                }

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                }
            });


            wm.addView(sv, params);

        } else {
            Log.d("TAG", "==== get Camera from service failed");
        }
    } else {
        Log.d("TAG", "==== There is no camera hardware on device.");
    }

    return super.onStartCommand(intent, flags, startId);
}

Util.java

的一部分
public static Camera getCameraInstance() {
    Camera c = null;
    try {
        c = Camera.open(1);
    } catch (Exception e) {
        Log.d("TAG", "Open camera failed: " + e);
    }
    return c;
}

更新 我更改了下面显示的两条线,切换到后置摄像头并录制好视频。

CameraService.java mMediaRecorder.setProfile(CamcorderProfile.get(1,CamcorderProfile.QUALITY_HIGH)); =&gt; mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

Util.java c = Camera.open(1); =&gt; c = Camera.open();

1 个答案:

答案 0 :(得分:0)

我终于偶尔解决了这个问题。

mMediaRecorder.setProfile(CamcorderProfile.get(1,CamcorderProfile.QUALITY_HIGH)); ==&gt; mMediaRecorder.setProfile(CamcorderProfile.get(1,CamcorderProfile.QUALITY_LOW));

现在效果很好。但是我不确切地知道它为什么会起作用以及之前为什么它不起作用?