Use MediaRecorder to write applications that capture audio and video from the device's microphone and camera, save the audio, and play it (using MediaPlayer).
1. Add permissions:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
2. Dynamically add permissions
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO},
100);
3. Create a MediaRecorderActivity, use Intent to connect MainActivity and MediaRecorderActivity
4. Steps to create MediaRecorder () source code:
(1) Create a recorder object: new MediaRecorder()
(2), set the direction of recording video: setCamera(camera); where camera is
Camera camera = Camera.open(); camera.setDisplayOrientation( ); camera.unlock();
(3) Set the audio collection method: setAudioSource(); set the video collection method: setVideoSource()
(4), set the video output format: setOutputFormat()
(5), set the audio encoding format: setAudioEncoder(); set the video encoding format: setVideoEncoder()
(6), set the direction of video playback: setOrientationHint()
(7), set the location of the output file: setOutputFile()
(8), set the size of the output video: setVideoSize()
(9), set the frame rate of the video: setVideoFrameRate()
(10), set video preview: setPreviewDisplay()
(11), ready to record: prepare()
(12), start recording: start()
(13), end recording: stop()
(14), release the recorder object: release()
(15), close and release camera: stopPreview(), release()
Full code:
MainActivity layout file 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 layout file 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();
}
}
}
It is recommended to use the real device test