Andorid custom camera, click to take photo, long press to record and call system camera to record (camera series 2)


Preface: This chapter is a derivative of the camera series, all based on camera customization. There may be a lot of information or articles on the Internet, but after reading these two articles, you must have learned to customize the camera (like WeChat click to take pictures, long press to record); and I think the biggest advantage of my article is to use the simplest Language to describe the code. Then the following is also illustrated. If you do not see the camera a series , it is recommended to see. I will directly start talking about the video here.

Section 2 of this chapter:

Customize camera to take pictures, and call system camera-Camera series (1)

Customize camera to record, and call system recording-Camera series (2)

Let's take a look at the final effect

Click to take a photo Long press to record video

1. Click to take a photo

This chapter only talks about video recording, click to take a picture, please see the camera series (1)


Second, record video

Video recording is based on the previous camera series (1). So it is best to read the previous article again. First, the system provides the MediaRecorder class for us to record videos. Come directly to our previous CameraInterface management class. Remember to follow this order, and remember to add the camera permissions and audio permissions before

2.1, prepare to record video, prepareVideoRecorder (SurfaceHolder surfaceHolder)

    public boolean prepareVideoRecorder(SurfaceHolder surfaceHolder) {
    
    
        //视频存放的路径
        videoPath = context.getFilesDir().getAbsolutePath().toString() + "/" + TimeUtils.getDateToStringLeo(System.currentTimeMillis() + "") + "_atmancarm.mp4";

        mMediaRecorder = new MediaRecorder();
        // Step 1: Unlock and set camera to MediaRecorder
        mCamera.unlock();
        //将camera设置给MediaRecorder
        mMediaRecorder.setCamera(mCamera);

        // Step 2: Set sources
        //设置音频资源,所以记得打开音频权限android.permission.RECORD_AUDIO
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        //设置录像资源
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

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

        // Step 4: Set output file
        mMediaRecorder.setOutputFile(videoPath);

        // Step 5: Set the preview output
        mMediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
        
        //这里和拍照一个概念,如果不旋转方向,那么拍摄出来的视频都是横屏的。
        //这里唯一存在的问题的,前置录制视频时会出现镜像问题。可以利用FFmpeg解决。本章讲的是录制,这个问题就留着了
        if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
    
    
            mMediaRecorder.setOrientationHint(90);
        } else if (cameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) {
    
    
            mMediaRecorder.setOrientationHint(270);
        }


        // Step 6: Prepare configured MediaRecorder
        try {
    
    
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
    
    
            //释放mMediaRecorder
            releaseMediaRecorder();
            return false;
        } catch (IOException e) {
    
    
            releaseMediaRecorder();
            return false;
        }
        return true;
    }

2.2. Start recording, startRecord()

    public void startRecord() {
    
    
        //录制
        mMediaRecorder.start();
    }

2.3. Stop recording, stopRecod()

public String stopRecod() {
    
    
        //停止录制,即是录制完成,我们将路径return出去。如果大家想做视频编辑,可以去了解FFmpeg
        mMediaRecorder.stop();
        return videoPath;
    }

2.4、释放mMediaRecorder,releaseMediaRecorder()

    public 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
        }
    }

2.5, simply record our video

Because I put all the management code in CameraInterface for code separation, and at the same time customize CameraView to call, avoid our activity directly appearing CameraInterface, so the code is better and clearer.

Record video directly call: cameraView.startRecord();
directly call after recording: String path = binding.cameraView.stopRecod(); path is the video path


Three, preview video interface

Originally, the following effects can be achieved by using the system's MediaController with VideoView. However, it is ugly, so I customized one with threads and ui.

Pay attention to a few points when using VideoView

  • Set playback path
//如果是网络路径,那么转换成uri
videoView.setVideoPath(path);

  • Pause and play
 //开始播放
 binding.videoView.start();
 //暂停
 binding.videoView.pause();
 //跳都某个时刻播放,毫秒数
 binding.videoView.seekTo(currentPosition);

  • Set loop playback
        binding.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    
    
            @Override
            public void onPrepared(MediaPlayer mp) {
    
    
                //设置循环播放
                mp.setLooping(true);
            }
        });

  • freed
 @Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        binding.videoView.suspend();
    }

  • If the playback path is wrong, it will pop up that the video cannot be played. When we click OK, it can't be played, then cancel the system and add your own. Exit the interface after clicking OK
binding.videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
    
    
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
    
    
                // 这里加上你自定义的dialog作操作
                // 这里return true后,就不弹出系统的无法播放此视频的弹窗
                return true;
            }
        });

4. Call the system camera to record and get the recording video path

Call the following code directly

    Intent intentVideo = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    startActivityForResult(intentVideo, CAMERA_VIDEO_PATH);

Get the video path in onActivityResult

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    
    
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
    
    
            if (requestCode == CAMERA_VIDEO_PATH) {
    
    
                //获取视频路径
                String path = data.getData().toString();
                VideoPlayActivity.startActivity(SystemCameraActivity.this, path);
            }
        }
    }

This github address

If you are also a love to learn and want to learn. Then go hand in hand, below is my official account

Guess you like

Origin blog.csdn.net/leol_2/article/details/104985000