Android-视频录制

微信图片_20190509182917.jpg

1、、添加依赖&权限

  implementation 'com.otaliastudios:cameraview:2.0.0-beta04'
  implementation 'androidx.lifecycle:lifecycle-common:2.2.0-alpha01'
  implementation 'androidx.annotation:annotation:1.0.2'
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

2、布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@android:color/transparent">

    <com.otaliastudios.cameraview.CameraView
        android:id="@+id/cv_video_recording"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:keepScreenOn="true"
        app:cameraFacing="front"
        app:cameraMode="video"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        android:src="@mipmap/img_scan"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription,MissingConstraints" />
    <Button
        android:id="@+id/btn_time_count_down_click_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_start"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_video_recording_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>

3、presenter文件

package com.example.zhang.mvp.presenter;

import com.example.zhang.base.BaseModel;
import com.example.zhang.base.BasePresenter;
import com.example.zhang.mvp.contract.VideoRecordingContract;
import com.example.zhang.mvp.ui.VideoRecordingActivity;
import com.otaliastudios.cameraview.CameraView;

import java.io.File;

/**
 * @author : zzh
 * @date : 2019/5/9
 * @desc :
 */
public class VideoRecordingPresenter extends BasePresenter<VideoRecordingContract.IVideoRecordingView, BaseModel> {
    public VideoRecordingPresenter(VideoRecordingContract.IVideoRecordingView view) {
        super(view);
    }

    /**
     * 开始录制视频
     */
    public void startTakeCamera(CameraView camera) {
        VideoRecordingActivity activity = (VideoRecordingActivity) view;
        camera.takeVideo(new File(activity.getExternalCacheDir(), "video.mp4"), 10000);
    }
}

4、Activity文件

package com.example.zhang.mvp.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;

import com.example.zhang.R;
import com.example.zhang.base.BaseActivity;
import com.example.zhang.mvp.contract.VideoRecordingContract;
import com.example.zhang.mvp.presenter.VideoRecordingPresenter;
import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.VideoResult;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : zzh
 * @date : 2019/5/9
 * @desc :视频录制
 */
public class VideoRecordingActivity extends BaseActivity<VideoRecordingPresenter> implements VideoRecordingContract.IVideoRecordingView {
    @BindView(R.id.cv_video_recording)
    CameraView cvVideoRecording;
    @BindView(R.id.tv_video_recording_address)
    TextView tvVideoRecordingAddress;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_recording);
        ButterKnife.bind(this);
        cvVideoRecording.addCameraListener(new CameraListener() {
            @Override
            public void onVideoTaken(@NonNull VideoResult result) {
                super.onVideoTaken(result);
                tvVideoRecordingAddress.setText(result.getFile().getAbsolutePath());
            }
        });
        presenter = new VideoRecordingPresenter(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        cvVideoRecording.open();
    }

    @Override
    protected void onPause() {
        super.onPause();
        cvVideoRecording.close();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cvVideoRecording.destroy();
    }

    @OnClick({R.id.btn_time_count_down_click_start})
    void onClick(View v) {
        if (v.getId() == R.id.btn_time_count_down_click_start) {
            presenter.startTakeCamera(cvVideoRecording);
        }
    }

    @Override
    public void showFileAddress(String fileAddress) {
        tvVideoRecordingAddress.setText(fileAddress);
    }

}

5、注意事项

①不用自己去申请权限,camera组件已经包含
②编译版本28

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容