2

I have an application entirely in portrait mode. (iOS 5 and above) I have a video played using MPMoviePlayerController, now in this video i want that when user rotates the iPhone, the video should go to landscape mode(in fullscreen). When video ends , again the video should go into portrait mode. Code:

    -(void)PlayVideo:(NSURL*)videoUrl
{


    moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
    [moviePlayerController.view setFrame:CGRectMake(6, 69, 309, 196)];
    [self.view addSubview:moviePlayerController.view];
    //    moviePlayerController.fullscreen = YES;


    moviePlayerController.controlStyle = MPMovieControlStyleNone;
    [self.view bringSubviewToFront:self.shareView];
    [self.view bringSubviewToFront:self.qualityView];

    [moviePlayerController play];

    // Register to receive a notification when the movie has finished playing.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];

}

Rest of the app, i want in portrait only. How do i achieve this?

z22
  • 10,013
  • 17
  • 70
  • 126

2 Answers2

4

First you need to set Support interface orientation to Portrait as well as Landscape

As shows here

Now in every UIViewController you need to override these methods -

for iOS 5 -

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
    return NO;
}

for iOS 6

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

In UIViewController where you are going to add MPMoviePlayerController override -

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
Rahul Mane
  • 1,005
  • 18
  • 33
  • but by doing this, all other controls in the viewController having the moviePlayer will also be in landscape mode, i want only the movieplayer to be in landscape – z22 Jul 31 '13 at 09:27
  • 1
    then you can check in `- (BOOL)shouldAutorotate` (**iOS 5**) `-(NSUInteger)supportedInterfaceOrientations` (**iOS 6**) whether moviePlayer is full screen or not. If full screen then return `YES` otherwise return `NO`. Check using `[moviePlayer isFullscreen]` – Rahul Mane Jul 31 '13 at 09:54
  • and i should implement - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } in every controller? – z22 Jul 31 '13 at 10:17
  • unfortunately but yes. – Rahul Mane Jul 31 '13 at 10:21
  • when i write : `- (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }` , in the viewcontroller having moviePlayer, i can still rotate to landscape. How do i solve this? – z22 Jul 31 '13 at 10:22
  • Are you using UINavigationController for navigating through application? – Rahul Mane Jul 31 '13 at 10:26
  • no i am using revealSideViewController, no navogationController – z22 Jul 31 '13 at 10:27
  • check this - http://stackoverflow.com/questions/12520030/how-to-force-a-uiviewcontroller-to-portait-orientation-in-ios-6 . It helped me hope it will be helpful for you too. – Rahul Mane Jul 31 '13 at 10:37
  • i saw the link, but i haven't used navigation controller – z22 Jul 31 '13 at 10:54
0

I created Xcode project with sample of the video player. It can show video on full screen in landscape mode. You can download it . I hope it helps you.

And second. Instead of

[self.view bringSubviewToFront:self.shareView];
[self.view bringSubviewToFront:self.qualityView];

you should write like this:

    [moviePlayerController.view insertSubview: self.shareView
                                      atIndex: 2];

    [moviePlayerController.view insertSubview: self.qualityView
                                      atIndex: 2];

then shareView and qualityView appears on top of the movie player.

stosha
  • 2,108
  • 2
  • 27
  • 29