android camera 录制视频 (前后摄像头)

在这里插入图片描述

系统录制

   Intent intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
   intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,0);
   intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT,10485760L);
   //android 5.0 无效
   intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,10);
   startActivityForResult(intent,VIDEO_CAPTURE);   

本篇介绍MediaRecorder 和 SurfaceView 录制方法


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.Observer;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class MainActivity extends Activity implements SurfaceHolder.Callback {
   
    private SurfaceView surfaceView;
    private ImageView recorderBtn;
    private ImageView recorder_btn_car;
    private ImageView recorder_btn_s;
    private ImageView iv_confirm;
    private ImageView iv_relase;
    private VideoFullScreen video_view;
    private RelativeLayout rl_bottom;
    private TextView tv_time;
    private MediaRecorder recorder;
    private boolean isStart = false;
    private boolean recorded = false;
    private Camera camera;
    private SurfaceHolder holder;
    private String path;
    private Camera.Size size;
    private int facingBack =  Camera.CameraInfo.CAMERA_FACING_BACK;
    private boolean isBack = true;
    private boolean isSource = true;
    private String mCurrentPath;
    private int mWidth = 1280;
    private int mHeight = 720;
    private static final int REQUEST_PERMISSION = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        //全屏(默认有个电量栏)
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        getWindow().setFormat((PixelFormat.TRANSLUCENT));
        setContentView(R.layout.activity_main);
        init();
        Display display = getWindowManager().getDefaultDisplay();
        mWidth = display.getHeight();
        mHeight= display.getWidth();
//        DisplayMetrics dm = getResources().getDisplayMetrics();
//         prevHeight = dm.widthPixels;
//         prevWidth =  prevHeight * mWidth/mHeight;

        recorderBtn.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick(View v) {
   
                if (!isStart) {
   
                    //未开始,点击开始
                    startTimer(16);
                    recorderBtn.setSelected(true);
                    recorder_btn_car.setVisibility(View.GONE);
                    recorder_btn_s.setVisibility(View.GONE);
                    startRecorder();
                    isStart = true;
                } else {
   
                    recorderBtn.setSelected(false);
                    recorder_btn_car.setVisibility(View.VISIBLE);
                    recorder_btn_s.setVisibility(View.GONE);
                    if (mDisposable !=null) {
   
                        mDisposable.cancel();
                        mDisposable = null;
                    }
                    //已开始,点击停止
                    stopRecorder();
                    startVideo();
                    isStart = false;
                }
            }
        });

        recorder_btn_car.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick(View view) {
   
                isBack = !isBack;
                if (isBack) {
   
                    facingBack = Camera.CameraInfo.CAMERA_FACING_BACK;
                }else  {
   
                    facingBack =   Camera.CameraInfo.CAMERA_FACING_FRONT;
                }
                releaseCamera();
                initPreview();
//                stopRecorder();
//                startRecorder();
            }
        });
        recorder_btn_s.setSelected(true);
        recorder_btn_s.setOnClickListener(new View.OnClickListener(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曾老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值