打开应用程序时如何自动录制视频?

时间:2014-01-29 08:16:56

标签: android

这是我的代码,当我打开应用程序时,我试图录制视频,但是当我试图打开应用程序时它会给我错误....... 使用按钮录制视频没有问题但是当我添加(启动时)方法时它就开始了 给我这个错误......

private Camera myCamera;
private MyCameraSurfaceView myCameraSurfaceView;
private MediaRecorder mediaRecorder;

Button myButton;
SurfaceHolder surfaceHolder;
boolean recording;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    recording = false;

    setContentView(R.layout.main);

    //Get Camera for preview
    myCamera = getCameraInstance();
    if(myCamera == null){
        Toast.makeText(AndroidVideoCapture.this, 
                "Fail to get Camera", 
                Toast.LENGTH_LONG).show();
    }

    myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
    FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
    myCameraPreview.addView(myCameraSurfaceView);

    myButton = (Button)findViewById(R.id.mybutton);
    myButton.setOnClickListener(myButtonOnClickListener);



}


@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

        if(!prepareMediaRecorder()){
        mediaRecorder.start();
}
}


Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(recording){
            // stop recording and release camera
            mediaRecorder.stop();  // stop the recording
            releaseMediaRecorder(); // release the MediaRecorder object

            //Exit after saved
            finish();
        }else{

            //Release Camera before MediaRecorder start
            releaseCamera();

            if(!prepareMediaRecorder()){
                Toast.makeText(AndroidVideoCapture.this, 
                        "Fail in prepareMediaRecorder()!\n - Ended -", 
                        Toast.LENGTH_LONG).show();
                finish();
            }

            mediaRecorder.start();
            recording = true;
            myButton.setText("STOP");
        }
    }};

private Camera getCameraInstance(){
    // TODO Auto-generated method stub
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

private boolean prepareMediaRecorder(){
    myCamera = getCameraInstance();
    mediaRecorder = new MediaRecorder();

    myCamera.unlock();
    mediaRecorder.setCamera(myCamera);

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

    mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
    mediaRecorder.setMaxDuration(6000); // Set max duration 60 sec.
    mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M

    mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());

    try {
        mediaRecorder.prepare();

    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        return false;
    }
    return true;

}

@Override
protected void onPause() {
    super.onPause();
    releaseMediaRecorder();       // if you are using MediaRecorder, release it first
    releaseCamera();              // release the camera immediately on pause event
}

private void releaseMediaRecorder(){
    if (mediaRecorder != null) {
        mediaRecorder.reset();   // clear recorder configuration
        mediaRecorder.release(); // release the recorder object
        mediaRecorder = null;
        myCamera.lock();           // lock camera for later use
    }
}

private void releaseCamera(){
    if (myCamera != null){
        myCamera.release();        // release the camera for other applications
        myCamera = null;
    }
}









public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{

    private SurfaceHolder mHolder;
    private Camera mCamera;

    public MyCameraSurfaceView(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int weight,
            int height) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // make any resize, rotate or reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }
}

}

这是我的logcat:

01-30 07:42:20.244:D / AndroidRuntime(267):关闭虚拟机

01-30 07:42:20.244:W / dalvikvm(267):threadid = 1:线程退出时未捕获异常(组= 0x4001d800)

01-30 07:42:20.254:E / AndroidRuntime(267):致命异常:主

01-30 07:42:20.254:E / AndroidRuntime(267):java.lang.RuntimeException:无法启动活动 ComponentInfo {com.exercise.AndroidVideoCapture / com.exercise.AndroidVideoCapture.AndroidVideoCapture}:java.lang.NullPointerException

01-30 07:42:20.254:E / AndroidRuntime(267):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

01-30 07:42:20.254:E / AndroidRuntime(267):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

01-30 07:42:20.254:E / AndroidRuntime(267):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)

01-30 07:42:20.254:E / AndroidRuntime(267):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)

01-30 07:42:20.254:E / AndroidRuntime(267):在android.os.Handler.dispatchMessage(Handler.java:99)

01-30 07:42:20.254:E / AndroidRuntime(267):在android.os.Looper.loop(Looper.java:123)

01-30 07:42:20.254:E / AndroidRuntime(267):在android.app.ActivityThread.main(ActivityThread.java:4627)

01-30 07:42:20.254:E / AndroidRuntime(267):at java.lang.reflect.Method.invokeNative(Native Method)

01-30 07:42:20.254:E / AndroidRuntime(267):at java.lang.reflect.Method.invoke(Method.java:521)

01-30 07:42:20.254:E / AndroidRuntime(267):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)

01-30 07:42:20.254:E / AndroidRuntime(267):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

01-30 07:42:20.254:E / AndroidRuntime(267):at dalvik.system.NativeStart.main(Native Method)

01-30 07:42:20.254:E / AndroidRuntime(267):引起:java.lang.NullPointerException

01-30 07:42:20.254:E / AndroidRuntime(267):at com.exercise.AndroidVideoCapture.AndroidVideoCapture.prepareMediaRecorder(AndroidVideoCapture.java:115)

01-30 07:42:20.254:E / AndroidRuntime(267):at com.exercise.AndroidVideoCapture.AndroidVideoCapture.onStart(AndroidVideoCapture.java:62)

01-30 07:42:20.254:E / AndroidRuntime(267):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)

01-30 07:42:20.254:E / AndroidRuntime(267):在android.app.Activity.performStart(Activity.java:3781)

01-30 07:42:20.254:E / AndroidRuntime(267):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)

01-30 07:42:20.254:E / AndroidRuntime(267):... 11更多

01-30 07:42:23.314:I / Process(267):发送信号。 PID:267 SIG:9

2 个答案:

答案 0 :(得分:0)

尝试将代码放入OnCreate中开始录制

告诉我它是否有效

答案 1 :(得分:0)

onStart()方法中,为什么使用NOT运算符?

使用prepareMediaRecorder()代替!prepareMediaRecorder()

您想要检查prepareMediaRecorder()是否返回TRUE(表示您已准备好录制),而不是“不正确”......

尝试删除“!”

希望这会有所帮助!!