MediaRecorder旋转SurfaceView以匹配屏幕方向

时间:2017-04-20 14:45:16

标签: android android-mediarecorder

我目前正在尝试使用媒体录制器创建录像机。视频的方向逆时针旋转90度(基本上是横向模式)。我希望它能够在纵向视图中拍摄视频。

您可以从我的代码中看到,我没有直接调用Camera变量在媒体记录器中设置,因为它表示代码已被弃用。谁能帮帮我吗。我的代码如下

   public void recordVideo()
   {
        isRecording = false;
        mediaRecorder = new MediaRecorder();
        initMediaRecorder();

        //recorderView is my surfaceView//
        surfaceHolder = recorderView.getHolder();
        surfaceHolder.addCallback(surfaceCallback);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        toggleRecordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if(isRecording)
                {
                    mediaRecorder.stop();
                    timer.cancel();
                    mediaRecorder.release();
                    toggleRecordButton.setText("START");

                    mediaRecorderLayout.setVisibility(View.GONE);
                    mPictureHelper.fetchCompressedUri(Environment.getExternalStorageDirectory().getPath()+"/myVideo.mp4");
                }
                else
                {
                    seconds = 0;
                    mediaRecorder.start();
                    timer = new Timer();
                    timer.scheduleAtFixedRate(new TimerTask() {
                        @Override
                        public void run()
                        {
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run()
                                {
                                    if (seconds < 10)
                                    {
                                        tvTimer.setText("00:0"+seconds);
                                    }
                                    else
                                    {
                                        tvTimer.setText("00:"+seconds);
                                    }
                                    seconds++;
                                }
                            });
                        }
                    },0,1000);

                    isRecording = true;
                    toggleRecordButton.setText("STOP");
                }
            }
        });
    }

    private void initMediaRecorder()
{
    String path = Environment.getExternalStorageDirectory().getPath()+"/myVideo.mp4";

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
    mediaRecorder.setProfile(camcorderProfile_HQ);
    mediaRecorder.setOutputFile(path);

    mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
    mediaRecorder.setMaxFileSize(10000000);
}

SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        prepareMediaRecorder();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
};
private void prepareMediaRecorder()
{
    mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
    try
    {
        mediaRecorder.prepare();
    }
    catch (IllegalStateException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我已经查看了有关此内容的其他帖子,但由于以前不推荐使用过很多代码,因此它们似乎都过时了。我希望有人能帮帮忙。感谢

1 个答案:

答案 0 :(得分:0)

在manifest.xml中添加

<activity
        android:name=".activity_class_name"
        android:screenOrientation="portrait" />