2017-04-20 89 views
0

我目前正在嘗試使用媒體記錄器創建錄像機。視頻方向逆時針旋轉90度(基本上是風景模式)。我希望它能夠在縱向視圖中拍攝視頻。MediaRecorder旋轉SurfaceView以匹配屏幕方向

您可以從我的代碼中看到,我沒有直接調用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(); 
    } 
} 

我已簽出對此的其他職位,但他們似乎都已經過時,由於這樣的事實,很多是用來使用的代碼已經被廢棄了。我希望有人能幫幫忙。由於

回答

0

中的manifest.xml

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

它使表面觀肖像添加這一點,但仍然獲得的視頻仍然在這裏旋轉 – Janwilx72

+0

看。可能幫助:http://stackoverflow.com/questions/30349897/android-video-orientation-changes-on-mediarecorder-start – Lcukerd