2013-04-30 81 views
1

我在下面有下面的代碼。問題是我將相機的方向設置爲縱向,即使預覽是縱向。但是當我播放那個視頻時,我在橫向模式下看到它。你們知道爲什麼以及如何解決這個問題嗎?有沒有辦法在播放時旋轉VideoView或在錄製後旋轉視頻本身? 我求求你,幫幫我,無論我問,沒有人知道爲什麼,以及如何解決它...設置縱向記錄,但橫向播放

下面是代碼:

public class BetaRecordActivity extends Activity { 

private Preview preview; 
private Camera camera; 
private MediaRecorder recorder; 

private int defaultCameraId; 
private int videoCount; 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.beta_record, menu); 
    return true; 
} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_beta_record); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    initCameraAndPreview(); 
    videoCount = 0; 

    preview.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       releaseCamera(); 
       getThatCamReady(); 
       recorder.start(); 
       videoCount ++; 
       break; 
      case MotionEvent.ACTION_MOVE: 
       break; 
      case MotionEvent.ACTION_UP: 
       stopRecording(); 
       break; 
      } 
      return true; 
     } 
    }); 

} 

@Override 
protected void onResume() { 
    super.onResume(); 

} 

@Override 
protected void onPause() { 
    super.onPause(); 

} 

private boolean getThatCamReady() { 
    camera = getCameraInstance(); 
    setCameraDisplayOrientation(this, defaultCameraId, camera); 
    camera.unlock(); 

    recorder = new MediaRecorder(); 
    recorder.setCamera(camera); 

    recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

    recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); 

    File tempFile = new File(Environment.getExternalStorageDirectory(), "/rec/temp/video_" + String.valueOf(videoCount) + ".mp4"); 
    recorder.setOutputFile(tempFile.getPath()); 
    recorder.setMaxDuration(12500); 
    recorder.setMaxFileSize(7000000); 

    recorder.setPreviewDisplay(preview.getHolder().getSurface()); 

    try { 
     recorder.prepare(); 
    } catch (IllegalStateException e) { 
     releaseMediaRecorder(); 
     return false; 
    } catch (IOException e) { 
     releaseMediaRecorder(); 
     return false; 
    } 

    return true; 
} 

private void stopRecording() { 
    //timer.pause(); 

    try { 
     recorder.stop(); 
    } catch(RuntimeException stopException) { 

     // Handle the cleanup here 
     //badList.add(videoCount); 
     //videoCount--; 
     //RecordActivity.this.timeLeft.setText(String.valueOf(videoCount)); 
    } 

    releaseMediaRecorder(); 
} 

private void initCameraAndPreview() { 
    // Find the total number of cameras available 
    int numberOfCameras = Camera.getNumberOfCameras(); 

    // Find the ID of the default camera 
    CameraInfo cameraInfo = new CameraInfo(); 
    for (int i = 0; i < numberOfCameras; i++) { 
     Camera.getCameraInfo(i, cameraInfo); 
     if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) { 
      defaultCameraId = i; 
     } 
    } 

    camera = getCameraInstance(); 
    setCameraDisplayOrientation(this, defaultCameraId, camera); 
    preview = new Preview(this, camera); 

    FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview); 
    myCameraPreview.addView(preview); 
} 

private void releaseMediaRecorder(){ 
    if (recorder != null) { 
     recorder.reset(); 
     recorder.release(); 
     recorder = null; 
     camera.lock(); 
    } 
} 

private void releaseCamera(){ 
    if (camera != null){ 
     camera.release(); 
     camera = null; 
    } 
} 

private Camera getCameraInstance(){ 
    // TODO Auto-generated method stub 
    Camera c = null; 
    try { 
     c = Camera.open(); // attempt to get a Camera instance 
    } 
    catch (Exception e){ 
     // Camera is not available (in use or does not exist) 
    } 
    return c; // returns null if camera is unavailable 
} 

public static void setCameraDisplayOrientation(Activity activity, 
     int cameraId, android.hardware.Camera camera) { 
    android.hardware.Camera.CameraInfo info = 
      new android.hardware.Camera.CameraInfo(); 
    android.hardware.Camera.getCameraInfo(cameraId, info); 
    int rotation = activity.getWindowManager().getDefaultDisplay() 
      .getRotation(); 
    int degrees = 0; 
    switch (rotation) { 
     case Surface.ROTATION_0: degrees = 0; break; 
     case Surface.ROTATION_90: degrees = 90; break; 
     case Surface.ROTATION_180: degrees = 180; break; 
     case Surface.ROTATION_270: degrees = 270; break; 
    } 

    int result; 
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
     result = (info.orientation + degrees) % 360; 
     result = (360 - result) % 360; // compensate the mirror 
    } else { // back-facing 
     result = (info.orientation - degrees + 360) % 360; 
    } 
    camera.setDisplayOrientation(result); 
} 

}

+0

嘗試添加語句'setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);''之前的setContentView(R.layout.activity_beta_record);' – 2013-04-30 13:44:10

+0

這似乎並不工作... – Ivanstame 2013-04-30 13:55:56

回答

1

你試過recorder.setOrientationHint( 90); ?

+0

嗯,這是很老的帖子,我是不知道我是否解決了它。但我想我試過了。 :) – Ivanstame 2016-04-20 09:18:50

相關問題