MediaRecorder怎么在竖屏的时候录像结果是横屏录的结果

Android 码拜 9年前 (2016-06-08) 2126次浏览
public class VideoActivity extends Activity implements SurfaceHolder.Callback {
    private Button start;// 开始录制按钮
    private Button stop;// 停止录制按钮
    private MediaRecorder mediarecorder;// 录制视频的类
    private SurfaceView surfaceview;// 显示视频的控件
    private SurfaceHolder surfaceHolder;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
////         设置横屏显示
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
////         选择支持半透明模式,在有surfaceview的activity中使用。
//        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.video);
        init();
    }
    private void init() {
        start = (Button) this.findViewById(R.id.start);
        stop = (Button) this.findViewById(R.id.stop);
        start.setOnClickListener(new TestVideoListener());
        stop.setOnClickListener(new TestVideoListener());
        surfaceview = (SurfaceView) this.findViewById(R.id.surfaceview);
        SurfaceHolder holder = surfaceview.getHolder();// 取得holder
        holder.addCallback(this); // holder加入回调接口
        // setType必须设置,要不出错.
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    Camera camera;
    class TestVideoListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            if (v == start) {
                mediarecorder = new MediaRecorder();// 创建mediarecorder对象
                // 设置录制视频源为Camera(相机)
                camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
                if (camera != null) {
//                    camera.setDisplayOrientation(90);//摄像图旋转90度
                    camera.unlock();
                    mediarecorder.setCamera(camera);
                }
                mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
                mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
                mediarecorder.setPreviewDisplay(surfaceHolder.getSurface());
//                mediarecorder.setOrientationHint(90);//视频旋转90度,没有用
                System.out.println("手机默认地址:" + Environment.getExternalStorageDirectory());
                // 设置视频文件输出的路径
                mediarecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/" + "test.mp4");
                try {
                    // 准备录制
                    mediarecorder.prepare();
                    // 开始录制
                    mediarecorder.start();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (v == stop) {
                if (mediarecorder != null) {
                    // 停止录制
                    mediarecorder.stop();
                    // 释放资源
                    mediarecorder.release();
                    mediarecorder = null;
                }
            }
        }
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        // 将holder,这个holder为开始在oncreat里面取得的holder,将它赋给surfaceHolder
        surfaceHolder = holder;
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // 将holder,这个holder为开始在oncreat里面取得的holder,将它赋给surfaceHolder
        surfaceHolder = holder;
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // surfaceDestroyed的时候同时对象设置为null
        surfaceview = null;
        surfaceHolder = null;
        mediarecorder = null;
    }
}

像做朋友圈视频那样的,竖着拿手机,然后压缩显示出横着拿手机拍摄的效果,现在代码显示出的还是竖屏拍摄结果,设置camera.setDisplayOrientation(90)现实的虽然是横屏,但明显是把竖屏给纵向压缩了,比例都变了。

解决方案

100

http://blog.csdn.net/licui1000/article/details/7591080
看下这个呢。

camera.setDisplayOrientation(90);//摄像图旋转90度
camera.unlock();
mediarecorder.setCamera(camera);
}
// 设置录制视频源为Camera(相机)
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediarecorder.setOrientationHint(90);//视频旋转90度

97

昨天本人也试了好长时间,也没找到办法,设置的一些参数只是对显示起作用,对应生成的视频不起作用,所以本人觉得还是在输出格式或录制到生成文件的那个步骤需要设置,但是没有找到地方。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明MediaRecorder怎么在竖屏的时候录像结果是横屏录的结果
喜欢 (0)
[1034331897@qq.com]
分享 (0)