标签:
最近在做一个项目需要运用到MediaRecorder的API,之前都没接触过这部分,开始着手弄的时候各种各样的问题,真是让人崩溃呀!
最后通过网上的资料和大神的指点,当然也有自己几天坚持不懈的努力,终于搞定了!
录像之前我采用Camera进行预览Camera.startPreview();预览的时候不知道为什么非常模糊,完全看不清,后来给Camera设置一些参数就清晰了,代码如下:
1 mParams = mCamera.getParameters(); 2 mParams.setPictureFormat(ImageFormat.JPEG);//设置拍照后存储的图片格式 3 mParams.setJpegQuality(100);//设置图片的质量 4 mParams.setJpegThumbnailQuality(100);//设置缩略图的质量 5 //设置PreviewSize和PictureSize 6 Size pictureSize = CamParaUtil.getInstance().getPropPictureSize(mParams.getSupportedPictureSizes()); 7 mParams.setPictureSize(pictureSize.width, pictureSize.height); 8 Size previewSize = CamParaUtil.getInstance().getPropPreviewSize( 9 mParams.getSupportedPreviewSizes(), previewRate, minWidth); 10 mParams.setPreviewSize(previewSize.width, previewSize.height);//设置预览SIze就清晰了 11 mCamera.setDisplayOrientation(90); 12 13 private Size getPropPreviewSize(List<Camera.Size> list, float th, int minWidth){ 14 15 int i = 0; 16 for(Size s:list){ 17 if((s.width >= minWidth) && equalRate(s, th)){ 18 Log.i(TAG, "PreviewSize:w = " + s.width + "h = " + s.height); 19 break; 20 } 21 i++; 22 } 23 if(i == list.size()){ 24 i = 0;//如果没找到,就选最小的size 25 } 26 return list.get(i); 27 } 28 29 private Size getPropPictureSize(List<Camera.Size> list){ 30 Size cameraSize = null; 31 if (list.size()>0) { 32 cameraSize=list.get(0); 33 for (Size size : list) { 34 //小于100W像素 35 if (size.width*size.height<100*10000) { 36 cameraSize=size; 37 break; 38 } 39 } 40 } 41 42 return cameraSize; 43 } 44 45 private boolean equalRate(Size s, float rate){ 46 float r = (float)(s.width)/(float)(s.height); 47 if(Math.abs(r - rate) <= 0.03) 48 { 49 return true; 50 } 51 else{ 52 return false; 53 } 54 }
解决了清晰度,接下来就要开始录像啦,录像时候要出现清晰度的问题,解决方法设置画面质量mediarecorder.setProfile,用这个方式的时候要注意版本兼容,除了兼容问题还有一个就是设置了这个setProfile,就不能设置输出格式和编码格式,代码如下:
1 if(mediarecorder == null) 2 mediarecorder = new MediaRecorder();// 创建mediarecorder对象 3 else 4 mediarecorder.reset(); 5 mCamera.stopPreview(); //这行代码是为了解决设置预览Size太大,开始录像画面定住的问题 6 mCamera.unlock(); 7 mediarecorder.setCamera(mCamera); 8 // 设置录制视频源为Camera(相机) 9 mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 10 mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //录音源为麦克风 11 //设置输出视频朝向,便于播放器识别。由于是竖屏录制,需要正转90° 12 mediarecorder.setOrientationHint(90); 13 boolean boo = false; 14 try { 15 // Set the recording profile. 16 CamcorderProfile profile = null; 17 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){ 18 if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_1080P)) 19 profile = CamcorderProfile.get(CamcorderProfile.QUALITY_1080P); 20 else if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)) 21 profile = CamcorderProfile.get(CamcorderProfile.QUALITY_720P); 22 else if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)) 23 profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P); 24 else if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_HIGH)) 25 profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); 26 if (profile != null){ 27 mediarecorder.setProfile(profile); 28 boo = true; 29 } 30 } 31 } catch (Exception e1) { 32 e1.printStackTrace(); 33 } 34 if(!boo){ 35 //设置了setProfile的话,下面的输出格式和编码需要注释 36 // 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4 37 mediarecorder 38 .setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 39 // 设置录制的视频编码h264 40 mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 41 mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);//音频编码 42 mediarecorder.setVideoEncodingBitRate(5*1024*1024); 43 mediarecorder.setVideoFrameRate(15); 44 } 45 mediarecorder.setPreviewDisplay(holder.getSurface()); 46 mediarecorder.setMaxDuration(15 * 1000); 47 48 mediarecorder.setOutputFile(videoFile.getPath()); 49 try { 50 // 准备录制 51 mediarecorder.prepare(); 52 // 开始录制 53 mediarecorder.start(); 54 } catch (IllegalStateException e) { 55 e.printStackTrace(); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 }
标签:
原文地址:http://www.cnblogs.com/kelina2mark/p/4915037.html