1 概述
MediaRecorder类用于录制音频和视频。步骤如下:
(1)实例化MediaRecorder对象
(2)设置声音来源和图像来源
(3)设置视频分辨率
(4)设置录制视频文件的保存路径
(5)设置使用哪个SurfaceView
(6)准备录制视频
(7)开始录制视频
(8)停止录制,释放资源
2 代码
CameraRecord:
https://github.com/hanyuhang-hz/android-demos
MainActivity.java
public class MainActivity extends Activity {
public final static String TAG = "CameraRecord";
private ImageButton play, stop, record;
private MediaRecorder mediaRecorder;
private SurfaceView surfaceView;
private boolean isRecord = false;
private File videoDir;
private android.hardware.Camera camera;
private File path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.RECORD_AUDIO}, 1);
}
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.CAMERA}, 1);
}
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
// 设置全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (!android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
Toast.makeText(MainActivity.this, "请安装SD卡!", Toast.LENGTH_SHORT).show();
}
record = (ImageButton) findViewById(R.id.record);
stop = (ImageButton) findViewById(R.id.stop);
play = (ImageButton) findViewById(R.id.play);
stop.setEnabled(false);
play.setEnabled(false);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceView.getHolder().setFixedSize(1920, 1080);
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
record();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRecord) {
mediaRecorder.stop();
mediaRecorder.release();
record.setEnabled(true);
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(MainActivity.this, "录像保存在:" + path, Toast.LENGTH_SHORT).show();
}
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 通过Intent跳转播放视频界面
Intent intent = new Intent(MainActivity.this, PlayVideoActivity.class);
startActivity(intent);
}
});
}
// 创建record()方法,实现录制功能
private void record() {
// 设置录制视频保存的文件夹
videoDir = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/");
if (!videoDir.exists()) {
videoDir.mkdir();
}
String fileName = "video.mp4";
path = new File(videoDir, fileName);
mediaRecorder = new MediaRecorder();
Camera.Parameters parameters = camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
camera.setParameters(parameters);
camera.startPreview();
camera.setDisplayOrientation(90);
camera.unlock();
mediaRecorder.setCamera(camera);
mediaRecorder.reset(); //重置MediaRecorder
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置麦克风获取声音
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // 设置摄像头获取图像
mediaRecorder.setVideoEncodingBitRate(1920 * 1080); // 设置清晰度
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(profile);
mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
mediaRecorder.setOutputFile(path.getAbsolutePath()); // 设置视频输出路径
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); // 设置使用SurfaceView预览视频
mediaRecorder.setOrientationHint(90); // 调整播放视频角度
try {
mediaRecorder.prepare(); // 准备录像
} catch (Exception e) {
e.printStackTrace();
}
mediaRecorder.start(); // 开始录制
Toast.makeText(MainActivity.this, "开始录像", Toast.LENGTH_SHORT).show();
record.setEnabled(false);
stop.setEnabled(true);
play.setEnabled(false);
isRecord = true;
}
@Override
protected void onResume() {
camera = android.hardware.Camera.open();
super.onResume();
}
@Override
protected void onPause() {
camera.stopPreview();
camera.release();
super.onPause();
}
}
PlayVideoActivity.java
public class PlayVideoActivity extends Activity {
private SurfaceView surfaceView;
private ImageButton play, pause, stop;
private MediaPlayer mediaPlayer;
private SurfaceHolder surfaceHolder;
private boolean noPlay = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video);
// 设置全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
play = (ImageButton) findViewById(R.id.play);
pause = (ImageButton) findViewById(R.id.pause);
stop = (ImageButton) findViewById(R.id.stop);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
pause.setEnabled(false);
stop.setEnabled(false);
/**
* 实现播放功能
*/
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noPlay) {
play();
noPlay = false;
} else {
mediaPlayer.start();
}
}
});
/**
* 实现暂停功能
*/
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
});
/**
* 实现停止功能
*/
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
noPlay = true;
pause.setEnabled(false);
stop.setEnabled(false);
}
}
});
}
/**
* 创建play()方法,在该方法中实现视频的播放功能
*/
public void play() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory() + "/DCIM/Camera/video.mp4");
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.start();
pause.setEnabled(true);
stop.setEnabled(true);
// 为MediaPlayer对象添加完成事件监听器
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(PlayVideoActivity.this, "视频播放完毕!", Toast.LENGTH_SHORT).show();
}
});
}
/**
* 当前Activity销毁时,停止正在播放的视频,并释放MediaPlayer所占用的资源
*/
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
// Activity销毁时停止播放,释放资源。不做这个操作,即使退出还是能听到视频播放的声音
mediaPlayer.release();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<!--SurfaceView组件-->
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--水平线性布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<!--播放按钮-->
<ImageButton
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/btn_bg"
android:src="@drawable/play" />
<!--录制按钮-->
<ImageButton
android:id="@+id/record"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/btn_bg"
android:src="@drawable/record" />
<!--停止按钮-->
<ImageButton
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/btn_bg"
android:src="@drawable/stop" />
</LinearLayout>
</RelativeLayout>
activity_play_video.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hyh.camerarecord.PlayVideoActivity">
<!--SurfaceView组件-->
<SurfaceView
android:id="@+id/surfaceView"
android:layout_weight="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--水平线性布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<!--播放按钮-->
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/black"
android:src="@drawable/btn_play" />
<!--暂停按钮-->
<ImageButton
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/black"
android:src="@drawable/btn_pause" />
<!--停止按钮-->
<ImageButton
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/black"
android:src="@drawable/btn_stop" />
</LinearLayout>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hyh.camerarecord">
<!-- 授予程序录制声音的权限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- 授予程序使用摄像头的权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- 授予使用外部存储器的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CameraRecord"
android:requestLegacyExternalStorage="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PlayVideoActivity">
</activity>
</application>
</manifest>
3 遇到的问题
3.1 java.io.FileNotFoundException: /storage/emulated/0/Camera/DCIM/video.mp4: open failed: ENOENT (No such file or directory)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:requestLegacyExternalStorage="true"
</application>
3.2 java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.hyh.camerarecord.MainActivity.record(MainActivity.java:141)
屏蔽:
//mediaRecorder.setVideoSize(1920, 1080); //设置视频的尺寸
//mediaRecorder.setVideoFrameRate(10); //设置为每秒10帧
3.3 MediaRecorder录制的视频不聚焦
Camera.Parameters parameters = camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
camera.setParameters(parameters);
3.4 MediaRecorder录制的视频和预览视频清晰度不一致,录制的视频不清晰
参考https://blog.csdn.net/qunqunstyle99/article/details/83143939
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(profile);
mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);