0

I have an iPhone app, witch can record video. The problem is the orientation, it is always in portrait. I need it to detect the device orientation and then save the video in the right orientation.

// setup video recording
mRecordingDelegate = new RecordingDelegate();

// setup the input Video part
mCaptureVideoInput = new AVCaptureDeviceInput(AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Video), out mVideoError);

//Audio part
mCaptureAudioInput = new AVCaptureDeviceInput(AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Audio), out mAudioError);

// setup the capture session
mCaptureSession = new AVCaptureSession();
mCaptureSession.SessionPreset = AVCaptureSession.PresetMedium;
mCaptureSession.AddInput(mCaptureVideoInput);
mCaptureSession.AddInput(mCaptureAudioInput);

// setup the output
mCaptureOutput = new AVCaptureMovieFileOutput();
mCaptureOutput.MaxRecordedDuration = MonoTouch.CoreMedia.CMTime.FromSeconds(VideoLength, 1);

//add Output to session
mCaptureSession.AddOutput(mCaptureOutput);

// add preview layer 
mPrevLayer = new AVCaptureVideoPreviewLayer(mCaptureSession);
mPrevLayer.Frame = new RectangleF(0, 0, 320, 480);
mPrevLayer.BackgroundColor = UIColor.Clear.CGColor;
mPrevLayer.VideoGravity = "AVLayerVideoGravityResize";

// Show video output
mCaptureSession.CommitConfiguration();
mCaptureSession.StartRunning();

RecordingDelegate.Stopped += delegate {
    if(recording)
        OnBtnStopRecordingVideo();
};

// add subviews
this.InvokeOnMainThread (delegate
{
    this.View.Layer.AddSublayer (mPrevLayer);
});

1 Answers1

1

1. The Basics

Before we get into the nitty gritty let’s first have a recap of how interface orientation changes work and how you should respond to them. Firstly, if you want your view to auto-rotate you need to override the method shouldAutorotateToInterfaceOrientation: and return YES. If you want to only allow auto-rotation under a certain condition you can put the test for this condition in this method too.

This is pretty much the most basic thing you need to do to allow for auto-rotation but there are additional methods you can override that can be very useful. These methods are

willRotateToInterfaceOrientation

didRotateFromInterfaceOrientation

willAnimateFirstHalfOfRotationToInterfaceOrientation

willAnimateSecondHalfOfRotationFromInterfaceOrientation

The first 2 methods are very useful for doing pre- and post-processing of your rotation. You could perhaps initialize a view controller or add some views to your current view in the willRotateToInterfaceOrientation. The second 2 are pretty much self explanatory. If you want to perform additional operations during that particular phase of the rotation you can implement them too.

Another very useful code example for when working with view controller orientations is:

if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){

    //do some processing…

}else if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){

    //do different processing…
}
else if(UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation)){

    //do something
}

NB: Description pasted from here Please look the post for more details

2. This post will help you to change video orientation

Community
  • 1
  • 1
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • Arh I see. I do a switch case on interfaceOrientation in willRotateToInterfaceOrientation. I can here set the AVCaptureVideoPreviewLayer to the orientation. But how can I set the video orientation? And thank you for a good quick answer. – user1614141 Aug 22 '12 at 07:51