MediaRecorder在Android上开始视频捕获的问题

MediaRecorder在Android上开始视频捕获的问题

本文介绍了MediaRecorder在Android上开始视频捕获的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一款可以录制其用户视频的应用程序.因此,如果有一个摄像头,我需要使用前置摄像头捕获视频.我建立了一个相机预览,并且效果很好.我使用Android操作方法网站来构建MediaRecorder并进行设置.如果我使用CamcorderProfile,则在我调用start()时媒体服务器将死掉.如果我自己设置编码器,则媒体服务器会在启动时引发运行时异常,并显示消息启动失败:-19"我在这里找到了有关此主题的一些问题,但没有一个解决我的问题.我认为这可能与我没有使用后置摄像头有关.也许我没有找到合适的纪录片来构建适当的代码.我认为这不仅是我的问题,而且我很乐意获得更多有关相机使用的知识.我的代码如下:

I'm trying to develop an App that amongst other things can record videos from its User. So I need to capture the video with the front facing camera if there is one.I build a camera preview and this works fine.I used the Android How-To Sites to build a MediaRecorder and set it up.If I use a CamcorderProfile my Media Server dies when I call start().If I set up the encoder by myself the media server throws a runtime exception at start() with the message "start failed: -19"I found some questions about this topic here but none solved my problem.I think that this could be related to the fact that I'm not using the back-facing camera. Maybe I didn't found the right documentary to build the proper code. I think this isn't only my problem and I would be happy to get some more knowledge about the camera usage.My Code follows:

设置预览的onResume()

the onResume() where the preview is set up

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()

the used method checkCameraHardware()

private boolean checkCameraHardware(Context context){
    boolean ret = true;
    if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            ret = true;
    }
    else {
        ret = false;
        }
    return ret;
}

和方法getCameraInstance()

and the method 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回调内)

this code snippet shows where the error appears ( inside a onClick Callback )

if(prepareVideoRecorder()){
    mMediaRecorder.start(); //here the errors occure
    recording = true;
    //start recording
}

以及三种与MediaRecorder相关的方法:prepareVideoRecorder(),releaseMediaRecorder()和release Camera()

and the three MediaRecorder related methods: prepareVideoRecorder(), releaseMediaRecorder() and 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

}

}

推荐答案

我正在回答自己的问题,以帮助遇到相同问题的每个人.错误是如此愚蠢,以至于承认它有些尴尬.

I'm answering my own question to help everybody who has the same problem.The error was so stupid that it's a little embarrassing to admit it.

在准备音频和视频源时,我犯了一个错误的表面.

On preparing the audio and video sources I committed the wrong surface.

我有不同的SurfaceView,并将错误的SurfaceView的表面提交给MediaRecorder.这导致尝试将两个不同的源连接到地面,这是不可能的,并导致媒体服务器关闭.

I have different SurfaceViews and committed the surface of the wrong SurfaceView to the MediaRecorder. This resulted in the attempt to connect two different sources to the surface what is not possible and leads to a shutdown of the Media Server.

我在GalaxyPad 10.1上测试了我的应用程序,并且视频记录正常.我在Dalvik VM上测试了该应用程序,并且视频是黑白的,但是也可以使用.

I tested my App on a GalaxyPad 10.1 and the video recording works fine.I tested the App on the Dalvik VM and the Video is black/white but also works.

我希望这会有所帮助.

这篇关于MediaRecorder在Android上开始视频捕获的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 17:06