在Android中使用GLSurfaceview进行视频录制

时间:2016-03-23 06:43:47

标签: android android-camera android-mediarecorder glsurfaceview

我正在尝试使用Android中的GLSurfaceview录制视频。但每当我点击进行录制时,它都会在暂停时给出空指针异常。

这是我录制视频的代码

mCamera = new CameraLoader();
    buttonn_capture.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    try{

                        if (recording) {

                            mediaRecorder.stop(); // stop the recording

                            recording = false;
                        } else {

                            // Release Camera before MediaRecorder start
                            mCamera.releaseCamera();

                            if (!prepareMediaRecorder()) {

                                finish();
                            }

                            try {
                                mediaRecorder.prepare();
                            } catch (IllegalStateException | IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            mediaRecorder.start();
                            recording = true;
                            // myButton.setText("Cancel");
                        }
                    }catch(Exception ex){
                        Toast.makeText(getApplicationContext(), "Please tap and hold to record!",
                                Toast.LENGTH_LONG).show();
                        reload();
                    }
                }
            });

private boolean prepareMediaRecorder() {

        mCamera.mCameraInstance.unlock();
        mediaRecorder.setCamera(mCamera.mCameraInstance);
        // Step 2: Set sources

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


        mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),
                "MusicDubs");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();

        }

        timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault())
                .format(new Date());

        CameraInfo caminfo = new CameraInfo();
        mCamera.mCameraInstance.getCameraInfo(0, caminfo);
        if (caminfo.facing == CameraInfo.CAMERA_FACING_FRONT) {

            mediaRecorder.setProfile(CamcorderProfile.get(0,
                    CamcorderProfile.QUALITY_HIGH));
            mediaRecorder
                    .setOrientationHint(270);

        } else if (caminfo.facing == CameraInfo.CAMERA_FACING_BACK) {
            mediaRecorder.setProfile(CamcorderProfile.get(0,
                    CamcorderProfile.QUALITY_HIGH));
            mediaRecorder
                    .setOrientationHint(270);
            mediaRecorder.setOrientationHint(90);

        }
        //mediaRecorder.setCaptureRate(20);
        mediaRecorder.setVideoFrameRate(120);
        mediaRecorder.setOutputFile(mediaStorageDir.getPath() + "/"
                + "_" + timeStamp + ".mp4");

        // Step 5: Set the preview output
        mediaRecorder.setPreviewDisplay(glSurfaceView.getHolder()
                .getSurface());

        try {
            mediaRecorder.prepare();
        } catch (IllegalStateException e) {
            releaseMediaRecorder();
           // releaseMediaPlayer();
            return false;
        } catch (IOException e) {
            releaseMediaRecorder();
           // releaseMediaPlayer();
            return false;
        }
        return true;

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

关于活动暂停和恢复我添加了该代码

@Override
    protected void onResume() {
        super.onResume();
        mCamera.onResume();
    }
@Override
    protected void onPause() {
        mCamera.onPause();
        super.onPause();
    }

这是我的相机类加载相机

private class CameraLoader {

    private int mCurrentCameraId = 0;
    private Camera mCameraInstance;

    public void onResume() {
        setUpCamera(mCurrentCameraId);
    }

    public void onPause() {
        releaseCamera();
    }

    public void switchCamera() {
        releaseCamera();
        mCurrentCameraId = (mCurrentCameraId + 1) % mCameraHelper.getNumberOfCameras();
        setUpCamera(mCurrentCameraId);
    }

    private void setUpCamera(final int id) {
        mCameraInstance = getCameraInstance(id);
        Parameters parameters = mCameraInstance.getParameters();
        // TODO adjust by getting supportedPreviewSizes and then choosing
        // the best one for screen size (best fill screen)
        if (parameters.getSupportedFocusModes().contains(
                Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
            parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
        }
        mCameraInstance.setParameters(parameters);

        int orientation = mCameraHelper.getCameraDisplayOrientation(
                ActivityCamera.this, mCurrentCameraId);
        CameraInfo2 cameraInfo = new CameraInfo2();
        mCameraHelper.getCameraInfo(mCurrentCameraId, cameraInfo);
        boolean flipHorizontal = cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT;
        mGPUImage.setUpCamera(mCameraInstance, orientation, flipHorizontal, false);
    }

    /** A safe way to get an instance of the Camera object. */
    private Camera getCameraInstance(final int id) {
        Camera c = null;
        try {
            c = mCameraHelper.openCamera(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }

    private void releaseCamera() {
        mCameraInstance.setPreviewCallback(null);
        mCameraInstance.release();
        mCameraInstance = null;
    }
}

它的行为非常奇怪,我没理解为什么它给我空的指针异常onPause因为没有点在那里得到相机null。请告诉我我在哪里做错了。任何帮助将非常感激。谢谢:)

0 个答案:

没有答案