这是由于设置的分辨率手机设备不支持,如果设置了不支持的分辨率,运行时就会报错。
解决方法:
//获取支持的分辨率
public static int[]getVideoSize(Context context,Camera mCamera){
int cw=640;
int ch=480;
Camera.Parameters parameter=mCamera.getParameters();
List prviewSizeList = parameter.getSupportedPreviewSizes();
List videoSizeList = parameter.getSupportedVideoSizes();
parameter.setPreviewSize(prviewSizeList.get(0).width,prviewSizeList.get(0).height);
//降序排列
Collections.sort(videoSizeList, new Comparator() {
@Override
public int compare(Camera.Size lhs, Camera.Size rhs) {
if (lhs.width > rhs.width) {
return -1;
}else if (lhs.width == rhs.width) {
return 0;
}else {
return 1;
}
}
});
int index =0;
for(int i=0;i<videoSizeList.size();i++){
if(videoSizeList.get(i).width<prviewSizeList.get(0).width&&videoSizeList.get(i).width>=640){
index= i;
}
}
cw = videoSizeList .get(index).width;
ch = videoSizeList .get(index).height;
int size[]=new int[2];
size[0]=cw;
size[1]=ch;
return size;
}
//初始化initMediaRecorder
private void initMediaRecorder() {
try {
mediarecorder =new MediaRecorder();
mediarecorder.setCamera(mCamera);
mediarecorder.reset();
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediarecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
// Set output file format
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
// 这两项需要放在setOutputFormat之后
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
int size[] = ToolUtils.getVideoSize(RecordVideoActivity.this, mCamera);
mediarecorder.setVideoSize(size[0], size[1]);
mediarecorder.setVideoFrameRate(30);
mediarecorder.setVideoEncodingBitRate(10 * size[0] * size[1]);
mediarecorder.setOrientationHint(90);
//设置记录会话的最大持续时间(毫秒)
mediarecorder.setMaxDuration(45 *1000);
mediarecorder.setPreviewDisplay(cameraPreview.getHolder().getSurface());
mediarecorder.setOutputFile(outFilePath);
}catch (Exception e) {
Log.e("Video",""+e.toString());
}
}
网友评论