Android Front Camera Record失败

时间:2015-03-20 07:48:41

标签: java android

我正在尝试使用MediaRecorder在我的一个活动中录制前置摄像头的音频。部分代码如下所示。如果我打开相机,它运行良好。但是,如果我打开前置摄像头,没有运行时错误,但我无法打开输出MP4

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)

一旦我在我的情况下得到相同的错误,我设置视频大小或错误。

protected void startRecording() {
        try {
            mrec = new MediaRecorder();
            mCamera.unlock();
            mrec.setCamera(mCamera);

            //Set audio source
            mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
            //set video source
            mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);

            //set output format
            mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

            int width = 320;
            int height = 240;
            try {
                //get the available sizes of the video
                List<Size> tmpList = getSupportedVideoSizes();

                final List<Size> sizeList = new Vector<Size>();

                // compare the apsect ratio of the candidate sizes against the
                // real ratio
                Double aspectRatio = (Double.valueOf(getWindowManager()
                        .getDefaultDisplay().getHeight()) / getWindowManager()
                        .getDefaultDisplay().getWidth());
                for (int i = tmpList.size() - 1; i > 0; i--) {
                    Double tmpRatio = Double.valueOf(tmpList.get(i).height)
                            / tmpList.get(i).width;
                    if (EnableLog.LOG_TAG) {
                        Log.e("Width & height", tmpList.get(i).width + " x "
                                + tmpList.get(i).height);
                    }
                    if (Math.abs(aspectRatio - tmpRatio) < .15) {
                        width = tmpList.get(i).width;
                        height = tmpList.get(i).height;
                        sizeList.add(tmpList.get(i));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            // set the size of video.
            // If the size is not applicable then throw the media recorder stop
            // -19 error
            mrec.setVideoSize(width, height);

            // Set the video encoding bit rate this changes for the high, low.
            // medium quality devices
            mrec.setVideoEncodingBitRate(1700000);

            //Set the video frame rate
            mrec.setVideoFrameRate(30);

            //set audio encoder format
            mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

            //set video encoder format
            mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

            //Show the display preview
            mrec.setPreviewDisplay(surfaceHolder.getSurface());

            //output file path
            mrec.setOutputFile(output_path);

            mrec.prepare();

            mrec.start();

        } catch (IllegalStateException e) {
            Crashlytics.logException(e);
            e.printStackTrace();
        } catch (IOException e) {
            Crashlytics.logException(e);
            e.printStackTrace();
        }
    }

public List<Size> getSupportedVideoSizes() {
        if (params.getSupportedVideoSizes() != null) {
            return params.getSupportedVideoSizes();
        } else {
            // Video sizes may be null, which indicates that all the supported
            // preview sizes are supported for video recording.
            return params.getSupportedPreviewSizes();
        }
    }

这对我有用。试试吧。也许它会有所帮助。我不确定试试这个。