在三星捕获视频时崩溃的应用程序

时间:2016-07-18 10:06:50

标签: android record video-capture

我正在尝试使用表面视图捕获视频它工作正常但是当我在三星设备中测试时应用程序崩溃,当我错了?任何帮助赞赏!!!

这是我的错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                            java.lang.RuntimeException: Fail to connect to camera service
                                                                                                at android.hardware.Camera.native_setup(Native Method)
                                                                                                at android.hardware.Camera.<init>(Camera.java:413)
                                                                                                at android.hardware.Camera.open(Camera.java:366)
                                                                                                at com.example.android.videorecordthumbnaildemo.MainActivity.initRecorder(MainActivity.java:76)
                                                                                                at com.example.android.videorecordthumbnaildemo.MainActivity.surfaceCreated(MainActivity.java:125)
                                                                                                at android.view.SurfaceView.updateWindow(SurfaceView.java:610)
                                                                                                at android.view.SurfaceView.access$000(SurfaceView.java:93)
                                                                                                at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:182)
                                                                                                at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:864)
                                                                                                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2142)
                                                                                                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
                                                                                                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
                                                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
                                                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:591)
                                                                                                at android.view.Choreographer.doFrame(Choreographer.java:561)
                                                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
                                                                                                at android.os.Handler.handleCallback(Handler.java:730)
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                                at android.os.Looper.loop(Looper.java:176)
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                                at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                                                at dalvik.system.NativeStart.main(Native Method

MainACtivity.java

class MainActivity extends Activity implements SurfaceHolder.Callback {
    //    private final String VIDEO_PATH_NAME = "/mnt/sdcard/VGA_30fps_512vbrate.mp4";
    private final String VIDEO_PATH_NAME = "/mnt/sdcard/VGA_30fps_512vbrate.mp4";
//    private final String VIDEO_PATH_NAME = Environment.getExternalStorageState() + "/VGA_30fps_512vbrate.mp4";

    private MediaRecorder mMediaRecorder;
    private Camera mCamera = null;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mHolder;
    private View mToggleButton;
    private boolean mInitSuccesful;
    private Button btnPlayVideo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);

        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mToggleButton = (ToggleButton) findViewById(R.id.toggleRecordingButton);
        mToggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            // toggle video recording
            public void onClick(View v) {
                if (((ToggleButton) v).isChecked()) {
                    try {
                        mMediaRecorder.start();
                        Thread.sleep(10 * 1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    finish();
                } else {
                    mMediaRecorder.stop();
                    mMediaRecorder.reset();
                    try {
                        initRecorder(mHolder.getSurface());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    /* Init the MediaRecorder, the order the methods are called is vital to
     * its correct functioning */
    private void initRecorder(Surface surface) throws IOException {
        // It is very important to unlock the camera before doing setCamera
        // or it will results in a black preview
        if (mCamera == null) {
            try {
                mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
                mCamera.setDisplayOrientation(90);
                mCamera.unlock();
            } catch (IllegalStateException e) {
                // This is thrown if the previous calls are not called with the
                // proper order
                e.printStackTrace();
            }
        }

        if (mMediaRecorder == null) mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setPreviewDisplay(surface);
        mMediaRecorder.setCamera(mCamera);

        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        //       mMediaRecorder.setOutputFormat(8);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

        mMediaRecorder.setVideoEncodingBitRate(3000000);
        mMediaRecorder.setOrientationHint(90);
      mMediaRecorder.setVideoFrameRate(CamcorderProfile.QUALITY_HIGH_SPEED_LOW);

//        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

//        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoFrameRate(24);
        mMediaRecorder.setVideoSize(720, 480);

        mMediaRecorder.setOutputFile(VIDEO_PATH_NAME);

        try {
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            // This is thrown if the previous calls are not called with the
            // proper order
            e.printStackTrace();
        }
        mInitSuccesful = true;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            if (!mInitSuccesful)
                initRecorder(mHolder.getSurface());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

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

    private void shutdown() {
        // Release MediaRecorder and especially the Camera as it's a shared
        // object that can be used by other applications
        mMediaRecorder.reset();
        mMediaRecorder.release();
        mCamera.release();

        // once the objects have been released they can't be reused
        mMediaRecorder = null;
        mCamera = null;
        mCamera.stopPreview();
        mCamera.setPreviewCallback(null);
    }
}

1 个答案:

答案 0 :(得分:0)

从Android开发者网站复制:

CAMERA                                                                          Added in API level 1

String CAMERA

Required to be able to access the camera device.

This will automatically enforce the } manifest element for all camera features. If you do not require all camera features or can properly operate if a camera is not available, then you must modify your manifest as appropriate in order to install on devices that don't support all camera features.

Protection level: dangerous

Constant Value: "android.permission.CAMERA" 

保护等级危险意味着您必须要求许可。它崩溃的原因是因为手机使用Android 6,这意味着该应用必须要求许可

在onCreate中加入:

    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M ) {
        checkPermission();
    }

这是在同一个班级:

private void checkPermission() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ){//Can add more as per requirement

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    123);

        }


    }