1 概述
MediaRecorder类用于录制音频和视频。步骤如下:
(1)实例化MediaRecorder对象
(2)设置声音来源和图像来源
(3)设置视频分辨率
(4)设置录制视频文件的保存路径
(5)设置使用哪个SurfaceView
(6)准备录制视频
(7)开始录制视频
(8)停止录制,释放资源
2 代码
CameraRecord:
/hanyuhang-hz/android-demos
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 camera;
private File path;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
if((, .RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED){
(, new String[]{ .RECORD_AUDIO}, 1);
}
if((, ) != PackageManager.PERMISSION_GRANTED){
(, new String[]{ }, 1);
}
if((, .WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
(, new String[]{ .WRITE_EXTERNAL_STORAGE}, 1);
}
// 设置全屏显示
getWindow().setFlags(.FLAG_FULLSCREEN, .FLAG_FULLSCREEN);
if (!().equals(
.MEDIA_MOUNTED)) {
(, "请安装SD卡!", Toast.LENGTH_SHORT).show();
}
record = (ImageButton) findViewById();
stop = (ImageButton) findViewById();
play = (ImageButton) findViewById();
(false);
(false);
surfaceView = (SurfaceView) findViewById();
().setFixedSize(1920, 1080);
(new () {
@Override
public void onClick(View v) {
record();
}
});
(new () {
@Override
public void onClick(View v) {
if (isRecord) {
();
();
(true);
(false);
(true);
(, "录像保存在:" + path, Toast.LENGTH_SHORT).show();
}
}
});
(new () {
@Override
public void onClick(View v) {
// 通过Intent跳转播放视频界面
Intent intent = new Intent(, );
startActivity(intent);
}
});
}
// 创建record()方法,实现录制功能
private void record() {
// 设置录制视频保存的文件夹
videoDir = new File(() + "/DCIM/Camera/");
if (!()) {
();
}
String fileName = "video.mp4";
path = new File(videoDir, fileName);
mediaRecorder = new MediaRecorder();
parameters = ();
(.FOCUS_MODE_CONTINUOUS_VIDEO);
(parameters);
();
(90);
();
(camera);
(); //重置MediaRecorder
(); // 设置麦克风获取声音
(); // 设置摄像头获取图像
(1920 * 1080); // 设置清晰度
CamcorderProfile profile = (CamcorderProfile.QUALITY_HIGH);
(profile);
(, );
(()); // 设置视频输出路径
(().getSurface()); // 设置使用SurfaceView预览视频
(90); // 调整播放视频角度
try {
(); // 准备录像
} catch (Exception e) {
();
}
(); // 开始录制
(, "开始录像", Toast.LENGTH_SHORT).show();
(false);
(true);
(false);
isRecord = true;
}
@Override
protected void onResume() {
camera = ();
();
}
@Override
protected void onPause() {
();
();
();
}
}
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) {
(savedInstanceState);
setContentView(.activity_play_video);
// 设置全屏显示
getWindow().setFlags(.FLAG_FULLSCREEN, .FLAG_FULLSCREEN);
play = (ImageButton) findViewById();
pause = (ImageButton) findViewById();
stop = (ImageButton) findViewById();
surfaceView = (SurfaceView) findViewById();
surfaceHolder = ();
(false);
(false);
/**
* 实现播放功能
*/
(new () {
@Override
public void onClick(View v) {
if (noPlay) {
play();
noPlay = false;
} else {
();
}
}
});
/**
* 实现暂停功能
*/
(new () {
@Override
public void onClick(View v) {
if (()) {
();
}
}
});
/**
* 实现停止功能
*/
(new () {
@Override
public void onClick(View v) {
if (()) {
();
();
noPlay = true;
(false);
(false);
}
}
});
}
/**
* 创建play()方法,在该方法中实现视频的播放功能
*/
public void play() {
mediaPlayer = new MediaPlayer();
(AudioManager.STREAM_MUSIC);
(surfaceHolder);
try {
(() + "/DCIM/Camera/video.mp4");
();
} catch (Exception e) {
();
}
();
(true);
(true);
// 为MediaPlayer对象添加完成事件监听器
(new () {
@Override
public void onCompletion(MediaPlayer mp) {
(, "视频播放完毕!", Toast.LENGTH_SHORT).show();
}
});
}
/**
* 当前Activity销毁时,停止正在播放的视频,并释放MediaPlayer所占用的资源
*/
@Override
protected void onDestroy() {
();
if (mediaPlayer != null) {
if (()) {
();
}
// Activity销毁时停止播放,释放资源。不做这个操作,即使退出还是能听到视频播放的声音
();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--SurfaceView组件-->
<SurfaceView
android:
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:
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/btn_bg"
android:src="@drawable/play" />
<!--录制按钮-->
<ImageButton
android:
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/btn_bg"
android:src="@drawable/record" />
<!--停止按钮-->
<ImageButton
android:
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:andro
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="">
<!--SurfaceView组件-->
<SurfaceView
android:
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:
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:
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:
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>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
package="">
<!-- 授予程序录制声音的权限 -->
<uses-permission android:name=".RECORD_AUDIO" />
<!-- 授予程序使用摄像头的权限 -->
<uses-permission android:name="" />
<!-- 授予使用外部存储器的权限 -->
<uses-permission android:name=".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/"
android:requestLegacyExternalStorage="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="" />
<category android:name="" />
</intent-filter>
</activity>
<activity android:name=".PlayVideoActivity">
</activity>
</application>
</manifest>
3 遇到的问题
3.1 : /storage/emulated/0/Camera/DCIM/video.mp4: open failed: ENOENT (No such file or directory)
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/>
<application
android:requestLegacyExternalStorage="true"
</application>
3.2 : start failed.
at (Native Method)
at (:141)
屏蔽:
//(1920, 1080); //设置视频的尺寸
//(10); //设置为每秒10帧
3.3 MediaRecorder录制的视频不聚焦
parameters = ();
(.FOCUS_MODE_CONTINUOUS_VIDEO);
(parameters);
3.4 MediaRecorder录制的视频和预览视频清晰度不一致,录制的视频不清晰
参考/qunqunstyle99/article/details/83143939
CamcorderProfile profile = (CamcorderProfile.QUALITY_HIGH);
(profile);
(, );