MediaRecorder类用于录制音频和视频。步骤如下:
(1)实例化MediaRecorder对象
(2)设置声音来源和图像来源
(3)设置视频分辨率
(4)设置录制视频文件的保存路径
(5)设置使用哪个SurfaceView
(6)准备录制视频
(7)开始录制视频
(8)停止录制,释放资源
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
activity_play_video.xml
AndroidManifest.xml
屏蔽:
//mediaRecorder.setVideoSize(1920, 1080); //设置视频的尺寸
//mediaRecorder.setVideoFrameRate(10); //设置为每秒10帧
Camera.Parameters parameters = camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
camera.setParameters(parameters);
参考https://blog.csdn.net/qunqunstyle99/article/details/83143939
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(profile);
mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);