0

我正在将谷歌的录像机示例实现到我的应用程序中,代码如下所示

    MediaRecorder recorder;
    Camera mCamera;
    SurfaceHolder holder;
    boolean isRecording = false;
    String TAG = "Evolution Camera";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);



        setContentView(R.layout.camera_view);
        SurfaceView cameraView = (SurfaceView) findViewById(R.id.camera_preview);


         // Add a listener to the Capture button
         ImageButton captureButton = (ImageButton) findViewById(R.id.btn_capture);
         captureButton.setOnClickListener(
             new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     if (isRecording) {
                         // stop recording and release camera
                         recorder.stop();  // stop the recording
                         releaseMediaRecorder(); // release the MediaRecorder object
                         mCamera.lock();         // take camera access back from MediaRecorder

                         // inform the user that recording has stopped
                      //   setCaptureButtonText("Capture");
                         isRecording = false;
                     } else {
                         // initialize video camera
                         if (prepareVideoRecorder()) {
                             // Camera is available and unlocked, MediaRecorder is prepared,
                             // now you can start recording
                             recorder.start();

                             // inform the user that recording has started
                        //     setCaptureButtonText("Stop");
                             isRecording = true;
                         } else {
                             // prepare didn't work, release the camera
                             releaseMediaRecorder();
                             // inform user
                         }
                     }
                 }
             });

         Log.d(TAG, "Setting holders");

        holder = cameraView.getHolder();
        holder.addCallback(this);
        Log.d(TAG, "Start preparing video recorder");
        prepareVideoRecorder();
    }

    private boolean prepareVideoRecorder(){

       // mCamera = getCameraInstance();
        mCamera = Camera.open();
        recorder = new MediaRecorder();

        // Step 1: Unlock and set camera to MediaRecorder
        mCamera.unlock();
        recorder.setCamera(mCamera);

        // Step 2: Set sources
        recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
        recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

        // Step 4: Set output file
        recorder.setOutputFile("/sdcard/videocapture_example.mp4");

        // Step 5: Set the preview output
        recorder.setPreviewDisplay(holder.getSurface());

        // Step 6: Prepare configured MediaRecorder
        try {
            recorder.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 static Camera getCameraInstance(){
        Camera c = null;
        int cnum = 0;
        int 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
    }

    @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 (recorder != null) {
            recorder.reset();   // clear recorder configuration
            recorder.release(); // release the recorder object
            recorder = null;
            mCamera.lock();           // lock camera for later use
        }
    }

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

    public void surfaceCreated(SurfaceHolder holder) {
        prepareVideoRecorder();
    }

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

    public void surfaceDestroyed(SurfaceHolder holder) {
        if (isRecording) {
            recorder.stop();
            isRecording = false;
        }
        recorder.release();
        finish();
    }

但是当我打开活动时,我会用这个日志强制关闭

    04-09 21:58:57.357: D/dalvikvm(2539): GC_EXPLICIT freed 682K, 14% free 39798K/46128K, paused 10ms+6ms, total 49ms
04-09 21:58:57.740: D/Evolution Launcher(2539): Pausing
04-09 21:58:57.802: D/Evolution Camera(2539): Setting holders
04-09 21:58:57.802: D/Evolution Camera(2539): Start preparing video recorder
04-09 21:58:58.068: E/MediaRecorderJNI(2539): Application lost the surface
04-09 21:58:58.068: D/Evolution Camera(2539): IOException preparing MediaRecorder: invalid preview surface
04-09 21:58:58.201: D/AndroidRuntime(2539): Shutting down VM
04-09 21:58:58.201: W/dalvikvm(2539): threadid=1: thread exiting with uncaught exception (group=0x41356930)
04-09 21:58:58.232: E/AndroidRuntime(2539): FATAL EXCEPTION: main
04-09 21:58:58.232: E/AndroidRuntime(2539): java.lang.RuntimeException: Fail to connect to camera service
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.hardware.Camera.native_setup(Native Method)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.hardware.Camera.<init>(Camera.java:340)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.hardware.Camera.open(Camera.java:317)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at com.doublep.evolution.EvoCamera.prepareVideoRecorder(EvoCamera.java:87)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at com.doublep.evolution.EvoCamera.surfaceCreated(EvoCamera.java:171)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.SurfaceView.updateWindow(SurfaceView.java:569)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.SurfaceView.access$000(SurfaceView.java:86)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:174)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:680)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1842)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.Choreographer.doFrame(Choreographer.java:532)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.os.Handler.handleCallback(Handler.java:725)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.os.Looper.loop(Looper.java:137)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at java.lang.reflect.Method.invokeNative(Native Method)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at java.lang.reflect.Method.invoke(Method.java:511)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-09 21:58:58.232: E/AndroidRuntime(2539):     at dalvik.system.NativeStart.main(Native Method)
04-09 21:59:34.802: D/Evolution Launcher(2539): Got location, location = Location[network 45.358237,10.737201 acc=20 et=+1d23h5m5s12ms {Bundle[mParcelledData.dataSize=600]}]
04-09 21:59:34.802: D/Evolution Launcher(2539): Getting longitude
04-09 21:59:34.802: D/Evolution Launcher(2539): Getting latitude
04-09 21:59:34.802: D/Evolution Launcher(2539): Getting altitude
04-09 21:59:34.990: W/dalvikvm(2539): threadid=11: thread exiting with uncaught exception (group=0x41356930)
04-09 21:59:34.990: I/Process(2539): Sending signal. PID: 2539 SIG: 9

这是我的清单

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

我忘了什么吗?

更新 camera_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Torch" >

    <ImageButton
        android:id="@+id/btn_capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="122dp"
        android:src="@drawable/ic_launcher" />

    <SurfaceView
        android:id="@+id/camera_preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

更新 2 Logcat 更改后

04-09 22:37:13.959: D/dalvikvm(6124): GC_EXPLICIT freed 983K, 5% free 20350K/21360K, paused 3ms+8ms, total 46ms
04-09 22:37:15.646: D/Evolution Launcher(6124): Pausing
04-09 22:37:15.685: D/Evolution Camera(6124): Setting holders
04-09 22:37:15.685: D/Evolution Camera(6124): Start preparing video recorder
04-09 22:37:15.951: E/MediaRecorderJNI(6124): Application lost the surface
04-09 22:37:15.951: D/Evolution Camera(6124): IOException preparing MediaRecorder: invalid preview surface
04-09 22:37:16.513: D/Evolution Launcher(6124): Stopping
04-09 22:37:19.638: D/AndroidRuntime(6124): Shutting down VM
04-09 22:37:19.638: W/dalvikvm(6124): threadid=1: thread exiting with uncaught exception (group=0x41356930)
04-09 22:37:19.677: E/AndroidRuntime(6124): FATAL EXCEPTION: main
04-09 22:37:19.677: E/AndroidRuntime(6124): java.lang.RuntimeException: Fail to connect to camera service
04-09 22:37:19.677: E/AndroidRuntime(6124):     at android.hardware.Camera.native_setup(Native Method)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at android.hardware.Camera.<init>(Camera.java:340)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at android.hardware.Camera.open(Camera.java:317)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at com.doublep.evolution.EvoCamera.prepareVideoRecorder(EvoCamera.java:88)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at com.doublep.evolution.EvoCamera.access$1(EvoCamera.java:85)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at com.doublep.evolution.EvoCamera$1.onClick(EvoCamera.java:60)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at android.view.View.performClick(View.java:4204)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at android.view.View$PerformClick.run(View.java:17355)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at android.os.Handler.handleCallback(Handler.java:725)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at android.os.Looper.loop(Looper.java:137)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at java.lang.reflect.Method.invokeNative(Native Method)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at java.lang.reflect.Method.invoke(Method.java:511)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-09 22:37:19.677: E/AndroidRuntime(6124):     at dalvik.system.NativeStart.main(Native Method)
04-09 22:37:21.482: I/Process(6124): Sending signal. PID: 6124 SIG: 9
04-09 22:37:21.677: D/dalvikvm(6552): GC_CONCURRENT freed 64K, 2% free 9002K/9100K, paused 5ms+2ms, total 17ms
04-09 22:37:21.677: D/dalvikvm(6552): WAIT_FOR_CONCURRENT_GC blocked 6ms
04-09 22:37:21.724: D/dalvikvm(6552): GC_CONCURRENT freed 4K, 1% free 9434K/9516K, paused 2ms+2ms, total 16ms
04-09 22:37:21.724: D/dalvikvm(6552): WAIT_FOR_CONCURRENT_GC blocked 3ms
04-09 22:37:21.748: D/dalvikvm(6552): GC_FOR_ALLOC freed 2K, 1% free 9543K/9620K, paused 10ms, total 10ms
04-09 22:37:21.748: I/dalvikvm-heap(6552): Grow heap (frag case) to 11.206MB for 1952260-byte allocation
04-09 22:37:21.763: D/dalvikvm(6552): GC_FOR_ALLOC freed <1K, 1% free 11450K/11528K, paused 17ms, total 17ms
04-09 22:37:21.779: D/dalvikvm(6552): GC_CONCURRENT freed <1K, 1% free 11449K/11528K, paused 2ms+4ms, total 17ms
04-09 22:37:21.849: D/dalvikvm(6552): GC_FOR_ALLOC freed <1K, 1% free 11905K/11980K, paused 11ms, total 11ms
04-09 22:37:21.920: D/dalvikvm(6552): GC_FOR_ALLOC freed 1K, 1% free 12809K/12884K, paused 12ms, total 12ms
04-09 22:37:21.990: D/dalvikvm(6552): GC_FOR_ALLOC freed 1K, 1% free 13712K/13788K, paused 11ms, total 11ms
04-09 22:37:22.076: D/dalvikvm(6552): GC_FOR_ALLOC freed 2K, 1% free 15068K/15144K, paused 9ms, total 10ms
04-09 22:37:22.131: D/dalvikvm(6552): GC_FOR_ALLOC freed 2K, 1% free 16787K/16864K, paused 10ms, total 10ms
04-09 22:37:22.232: D/dalvikvm(6552): GC_CONCURRENT freed 111K, 1% free 19333K/19484K, paused 2ms+2ms, total 13ms
04-09 22:37:22.326: D/Evolution Launcher(6552): Loading variables
04-09 22:37:22.326: D/Evolution Launcher(6552): Restoring prefs
04-09 22:37:22.334: D/Evolution Launcher(6552): Screen density actions
04-09 22:37:22.349: D/Evolution Launcher- CarLocation(6552): Getting car location
04-09 22:37:22.357: D/Evolution Launcher(6552): Resuming
04-09 22:37:22.412: D/libEGL(6552): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
04-09 22:37:22.427: D/libEGL(6552): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
04-09 22:37:22.427: D/libEGL(6552): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
04-09 22:37:22.537: D/OpenGLRenderer(6552): Enabling debug mode 0
04-09 22:37:24.920: D/dalvikvm(6552): GC_EXPLICIT freed 739K, 4% free 20266K/21032K, paused 2ms+6ms, total 45ms
04-09 22:37:27.357: D/dalvikvm(6552): GC_EXPLICIT freed 940K, 5% free 20349K/21320K, paused 3ms+9ms, total 45ms
04-09 22:37:29.677: D/dalvikvm(6552): GC_EXPLICIT freed 1046K, 6% free 20356K/21432K, paused 2ms+7ms, total 37ms
4

1 回答 1

1

你把这个添加到你的清单中了吗?

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

尝试将此添加到您的代码中:

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {
     if (mCam != null) {
        mCam.stopPreview();
        mCam.setPreviewCallback(null);
        mCam.release();
        mCam = null;
     }
   }

   @Override
   public void surfaceCreated(SurfaceHolder holder) {
       if (mCam == null) {
          mCam = Camera.open();
          try {
             mCam.setPreviewDisplay(holder);

             // TODO test how much setPreviewCallbackWithBuffer is faster
             mCam.setPreviewCallback(this);
          } catch (IOException e) {
             mCam.release();
             mCam = null;
          }
       }
   }

用于检查您是否正确释放了所有获取的相机资源

于 2013-04-09T20:04:54.993 回答