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);
}

}

4

1 回答 1

1

你试过 recorder.setOrientationHint(90); ?

于 2016-04-07T15:26:31.793 回答