-
(Android)项目中用到了MediaRecorder进行视频的录制,但是做的过程中不知是我的代码问题还是mui本身的问题 ,当我调用这个方法时mediarecorder.setPreviewDisplay(surfaceview.getHolder().getSurface());总是掉不出来,总是报错!
附上代码:
var ioOptions = plus.android.importClass('java.io.IOException');
var activityInfo = plus.android.importClass('android.content.pm.ActivityInfo');
var PixelFormat = plus.android.importClass('android.graphics.PixelFormat');
var Camera = plus.android.importClass('android.hardware.Camera');
var SurfaceView = plus.android.importClass('android.view.SurfaceView');
var MediaRecorder = plus.android.importClass('android.media.MediaRecorder');
var Bundle = plus.android.importClass('android.os.Bundle');
var SurfaceHolder = plus.android.implements('android.view.SurfaceHolder');
// var View = plus.android.importClass('android.view.View');
var LinearLayout = plus.android.importClass('android.widget.LinearLayout');
var Surface = plus.android.importClass('android.view.Surface');
var SurfaceTexture = plus.android.importClass('android.graphics.SurfaceTexture');
var main = plus.android.runtimeMainActivity();
var view = new LinearLayout(main);
//设置布局参数
var lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
console.log("1010101010" lp);
view.setLayoutParams(lp);//设置布局参数
console.log("2020202020");
var surfaceview = new SurfaceView(main);
console.log("30303030" surfaceview);
var linearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); //取控件surfaceview当前的布局参数
console.log("4040404004" linearParams);
view.addView(surfaceview,linearParams);
main.setContentView(view);
mediarecorder = new MediaRecorder();// 创建mediarecorder对象
//设置摄像头以及摄像头的方向
var CammeraIndex=FindBackCamera();//网上参考的一个函数,用来获取后置摄像头的info
console.log("1111111111");
var mCamera=Camera.open();
console.log("222222222");
mCamera.setDisplayOrientation(90);
mCamera.unlock();
console.log("33333");
console.log(surfaceview);
var SurfaceHolder = surfaceview.getHolder();// 取得holder
console.log("4444444");
// console.log(holder);
// holder.addCallback(main); // holder加入回调接口
// setType必须设置,要不出错.
mediarecorder.setCamera(mCamera);
// 设置录制视频源为Camera(相机)
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
mediarecorder .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置录制的视频编码h263 h264
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
// 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错
mediarecorder.setVideoSize(1280, 720);
// 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错
mediarecorder.setVideoFrameRate(20);
console.log("555555555555" surfaceview.getHolder());
mediarecorder.setPreviewDisplay(surfaceview.getHolder().getSurface());
console.log("66666666666" new Surface(new SurfaceTexture(0)));
// 设置视频文件输出的路径
mediarecorder.setOutputFile("/sdcard/love.3gp");
console.log("777777777");
// 准备录制
mediarecorder.prepare();
console.log("88888888888");
// 开始录制
mediarecorder.start();
console.log("9999999999");
1***@qq.com
解决了
2018-07-30 10:23
wnsuan
回复 1***@qq.com:可以分享一下解决方案吗,谢谢了
2018-08-08 14:30