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
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);
}
public void record(View view) {
Intent intent = new Intent(this, MediaRecoderActivity.class);
startActivity(intent);
}
}
MediaRecorderActivity
public class MediaRecoderActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private MediaRecorder mediaRecorder;
private SurfaceView surfaceView;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_recoder);
surfaceView = findViewById(R.id.surface);
button = findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String s = button.getText().toString();
if(s.equals("开始")){
button.setText("结束");
mediaRecorder = new MediaRecorder();
camera = Camera.open();
camera.setDisplayOrientation(90);//将摄像头预览的角度进行调整
camera.unlock();
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置音频源
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//设置视频源
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//设置视频输出格式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);//设置音频编码格式
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//设置视频编码格式
mediaRecorder.setOrientationHint(90);//设置视频播放的方向
mediaRecorder.setOutputFile(new File(getExternalFilesDir(""),"a.mp4")
.getAbsolutePath());//设置存储路径
mediaRecorder.setVideoSize(640,480);//大小
mediaRecorder.setVideoFrameRate(30);//帧率
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());//设置预览界面
try {
mediaRecorder.prepare();//准备录制
} catch (IOException e) {
e.printStackTrace();
}
mediaRecorder.start();//开始录制
}else{
button.setText("开始");
mediaRecorder.stop();//结束录制
mediaRecorder.release();//释放
camera.stopPreview();//关闭摄像头
camera.release();//释放
}
}
}
布局
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="录制视频"
android:onClick="record"
android:layout_gravity="center"
android:layout_marginTop="100dp"/>
</LinearLayout>
activity_media_recorder
<?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=".MediaRecoderActivity">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/surface"/>
<Button
android:id="@+id/button"
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>