Android MediaRecorder录制视频详细步骤

使用MediaRecorder能够编写从设备麦克风与相机捕获音视频,保存音频并(使用MediaPlayer)进行播放的应用。

1.添加权限:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />

2.动态添加权限

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

3、创建一个MediaRecorderActivity,使用Intent连接MainActivity和MediaRecorderActivity

4、创建 MediaRecorder ()源码即步骤:

(1)、创建recorder对象:new MediaRecorder()

(2)、设置录制视频的方向:setCamera(camera);其中camera为

       Camera camera = Camera.open();
       camera.setDisplayOrientation( );
       camera.unlock();

(3)、设置音频的采集方式:setAudioSource();设置视频的采集方式:setVideoSource()

(4)、设置视频的输出格式:setOutputFormat()

(5)、设置音频的编码格式:setAudioEncoder();设置视频的编码格式:setVideoEncoder()

(6)、设置视频播放的方向:setOrientationHint()

(7)、设置输出文件的位置:setOutputFile()

(8)、设置输出视频的大小:setVideoSize()

(9)、设置视频的帧率:setVideoFrameRate()

(10)、设置视频预览:setPreviewDisplay()

(11)、准备录制:prepare()

(12)、开始录制:start()

(13)、结束录制:stop()

(14)、释放recorder对象:release()

(15)、关闭与释放camera:stopPreview(),release()

完整代码:

MainActivity布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
	<Button
     	android:id="@+id/button"
     	android:layout_width="wrap_content"
     	android:layout_height="wrap_content"
     	android:text="录制视频"
     	android:onClick="record"
     	app:layout_constraintBottom_toTopOf="@+id/button2"
     	app:layout_constraintEnd_toEndOf="parent"
     	app:layout_constraintStart_toStartOf="parent"
     	app:layout_constraintTop_toTopOf="parent" />
    
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * 在 Android 6.0 以上需要动态添加权限
         */
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO},
                100);

    }

    /**
     * 录制视频
     * @param view
     */
    public void record(View view) {
        //跳转到MediaRecorderActivity完成录制视频
        startActivity(new Intent(this, MediaRecorderActivity.class));
    }
}

 MediaRecorderActivity.java布局文件activity_media_recorder.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MediaRecorderActivity">

    <!--显示当前预览的界面(录屏使用)-->
    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/surface"/>
    <!--开始/结束录屏按钮-->
    <Button
        android:id="@+id/btn_opt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 MediaRecorderActivity.java

package com.example.mediademo;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.IOException;

public class MediaRecorderActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button;
    private SurfaceView surfaceView;
    private MediaRecorder mediaRecorder;
    private Camera camera; //摄像头的一些配置

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

        //当前手机预览的页面
        surfaceView = findViewById(R.id.surface);
        //开始或结束按钮
        button = findViewById(R.id.btn_opt);
        //按钮点击事件
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //获取按钮文本内容
        String s = button.getText().toString();
        if(s.equals("开始")){
            //点击了开始则设置按钮文本值为结束
            button.setText("结束");
            /**
             * 开始录制
             */
            //1.创建recorder对象
            mediaRecorder = new MediaRecorder();
            //2.设置录制视频的方向
            camera = Camera.open();//摄像头对象
            camera.setDisplayOrientation(90);//将摄像头预览的角度进行调整
            camera.unlock();
            mediaRecorder.setCamera(camera);
            //3.设置音频源(MIC:麦克风采集音频)
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            //4.设置视频源(CAMERA:摄像头采集视频)
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            //5.设置视频输出格式(MP4)
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            //6.设置音频编码格式
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            //7.设置视频编码格式
            mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            //8.设置视频播放的方向
            mediaRecorder.setOrientationHint(90);
            //9.设置输出文件的位置
            mediaRecorder.setOutputFile(new File(getExternalFilesDir(""),"a.mp4")
                    .getAbsolutePath());
            //10.设置输出视频的大小
            mediaRecorder.setVideoSize(640,480);
            //11.设置视频的帧率
            mediaRecorder.setVideoFrameRate(30);
            //12.设置预览界面(设置一个Surface,也就是预览的页面,然后交给)
            mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
            try {
                //13.准备录制
                mediaRecorder.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //14.开始录制
            mediaRecorder.start();
        }else{
            //点击了结束则设置按钮文本值为开始
            button.setText("开始");
            //1.结束录制
            mediaRecorder.stop();
            //2.释放recorder对象
            mediaRecorder.release();
            //3.关闭摄像头
            camera.stopPreview();
            //4.释放
            camera.release();
        }
    }
}

建议使用真机测试 

猜你喜欢

转载自blog.csdn.net/weixin_53443677/article/details/126469100