什么是MediaRecorder
MediaRecorder是安卓提供的一个用于音视频采集的类
在前几篇文章中,我们已经介绍了如何进行音频和视频的采集,即通过AudioRecord采集音频,通过Camera来采集视频
那么我们还为什么要介绍MediaRecorder了
前几篇文章介绍的音视频采集,采集的都是原始数据,对于音频来说是pcm数据,对于视频来说是YUV数据,camera中获取的是NV21,camera2获取的是YUV_420_888
对于原始数据还需要进行编码和封装,才能使用
为了方便,google提供了MediaRecorder这个类,通过它会自动将原始数据进行编码和封装
MediaRecorder的优缺点
使用方便,得到就是编码和封装好的音视频文件,可以直接使用无法获取原始数据,从而无法原始数据添加一些自己的处理
1.开始录音
从上到下依次是(注意顺序很重要)
从麦克风采集声音数据, 设置采样率, 设置文件保存格式, 设置编码格式,设备文件保存路径
private void start(){
try {
mRecorder=new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setAudioSamplingRate(44100);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setOutputFile(path);
mRecorder.prepare();
mRecorder.start();
} catch (IOException e) {
e.printStackTrace();
2.结束录音
private void stop(){
ToastUtil.startShort(this,"录音文件已保存至:"+path);
mRecorder.stop();
mRecorder.release();
mRecorder=null;
视频录制(Camera1和Camera2)
单纯的音频录制很顺利,但是我在写视频录制的代码的过程中踩了很多坑,请读者们注意了,后面我会说道
Camera1
1.开始录制
public void startRecord(String path) {
if(TextUtils.isEmpty(path)){
Log.d(TAG,"Camera1 Record path is empty");
return;
mVideoPath=path;
setUpMediaRecorder();
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
Log.d(TAG,"Camera1 has start record");
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG,"Camera1 start failed:"+e.getMessage());
//这个方法的顺序很重要,并且一些设置还不能少
private void setUpMediaRecorder(){
mCamera.unlock();
mMediaRecorder=new MediaRecorder();
mMediaRecorder.reset();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setOutputFile(mVideoPath);
mMediaRecorder.setVideoEncodingBitRate(5*1024*1024);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(videoSize.width,videoSize.height);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
if(mCameraId==1) {
mMediaRecorder.setOrientationHint(270);
}else {
mMediaRecorder.setOrientationHint(90);
Log.i(TAG,"Camera1 has set MediaRecorder VideoSize:"+videoSize.width+"*"+videoSize.height);
Log.i(TAG,"Camera1 OutputFilePath:"+mVideoPath);
mMediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.d(TAG,"MediaRecorder error:"+what+"-"+extra);
2.结束录制
public void stopRecord() {
if(mMediaRecorder!=null){
mCamera.lock();
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder=null;
Log.d(TAG,"Camera1 has stop record");
Camera2
1.开始录制
public void startRecord(String path) {
if (TextUtils.isEmpty(path)) {
Log.d(TAG, "Camera2 Record path is empty");
return;
closeSession();
mVideoPath = path;
setUpMediaRecorder();
try {
final CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
Surface previewSurface = getSurface();
builder.addTarget(previewSurface);
Surface recordSurface = mMediaRecorder.getSurface();
builder.addTarget(recordSurface);
mCaptureRequest = builder.build();
List<Surface> surfaces = Arrays.asList(previewSurface, recordSurface);
// List<Surface> surfaces = Arrays.asList(previewSurface);
mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
mPreviewCaptureSession = session;
//设置反复捕获数据的请求,这样预览界面就会一直有数据显示
try {
mPreviewCaptureSession.setRepeatingRequest(mCaptureRequest, null, mCameraHandler);
mMediaRecorder.start();
} catch (Exception e) {
e.printStackTrace();
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}, mCameraHandler);
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "Camera2 startRecord failed:" + e.getMessage());
//和camera1的设置一样,参考的谷歌官方demo的设置顺序
private void setUpMediaRecorder() {
mMediaRecorder = new MediaRecorder();
mMediaRecorder.reset();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setOutputFile(mVideoPath);
mMediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
// mMediaRecorder.setVideoSize(1920,1080);
if (mCameraId == 1) {
mMediaRecorder.setOrientationHint(270);
} else {
mMediaRecorder.setOrientationHint(90);
try {
mMediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "Camera2 has set MediaRecorder VideoSize:" + mVideoSize.getWidth() + "*" + mVideoSize.getHeight());
Log.i(TAG, "Camera2 OutputFilePath:" + mVideoPath);
mMediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.d(TAG, "MediaRecorder error:" + what + "-" + extra);
2.结束录制
public void stopRecord() {
try {
//解决startPreview failed:Illegal state encountered in camera service
//https://stackoverflow.com/questions/27907090/android-camera-2-api
if (mPreviewCaptureSession != null) {
mPreviewCaptureSession.stopRepeating();
mPreviewCaptureSession.abortCaptures();
} catch (CameraAccessException e) {
e.printStackTrace();
if (mMediaRecorder != null) {
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
Log.d(TAG, "Camera2 has stop record");
// startPreview();
destroyCamera();
openCamera();
在分别用新旧Camera的api进行视频的录制的过程中,我踩了很多的坑,基本花了我2天时间才算彻底完成了camera1和camera2的视频录制代码
下面我就总结一下我所遇到的一些坑
1.MediaRecorder设置的顺序问题
在setUpMediaRecorder方法中,如果调整设置的先后顺序,你的程序会报start failed的错误,并且这错误是Native层的,目前我们并不能定位问题所在,更别说解决了
我的顺序的设置最终是参照谷歌官方的demo顺序
https://github.com/googlesamples/android-Camera2Video
但是需要注意的是这里是Camera2的demo,Camera1的顺序可以和Camera1设置成一样,但是注意要添加下面几句代码
mCamera.unlock();
mMediaRecorder=new MediaRecorder();
mMediaRecorder.reset();
mMediaRecorder.setCamera(mCamera);
2.Camera1问题
就是上面所说的在参考官方demo的顺序的同时添加那几个代码
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
其实在这个过程中,由于我开始在stopRecord方法中未调用
mCamera.lock();
上面这句代码,在我不停关闭和打开相机时,发现出现了camera在打开相机时出现了ANR问题
我的第一反应是将打开这个操作放在子线程,问题解决了,不过当我加上上面这句代码时,不放在子线程也不会出现ANR问题了
3.Camera2问题
顺序问题解决了之后,Camera1是没问题了,但是Camera2一直报一个错误
mMediaRecorder.getSurface()
就是上面这行代码,一直出现failed to getSurface
通过在官方demo代码上不停的试,才发现问题所在
mMediaRecorder.prepare();
即在调用getSurface之前一定要先调用prepare方法
视频录制完之后,并不能恢复预览
这个问题官方demo的写法本来就有bug
在调用stopRecord方法后,报错
startPreview failed:Illegal state encountered in camera service
解决办法,参考https://stackoverflow.com/questions/27907090/android-camera-2-api
public void stopRecord() {
try {
//解决startPreview failed:Illegal state encountered in camera service
//https://stackoverflow.com/questions/27907090/android-camera-2-api
if (mPreviewCaptureSession != null) {
mPreviewCaptureSession.stopRepeating();
mPreviewCaptureSession.abortCaptures();
} catch (CameraAccessException e) {
e.printStackTrace();
if (mMediaRecorder != null) {
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
Log.d(TAG, "Camera2 has stop record");
// startPreview();
destroyCamera();
openCamera();