2

When using my app users can click and watch videos displayed in a list. Once they click on that video they can change the orientation to either portrait or landscape. The list page is only portrait.

The error I am facing is when the user watch's a video in landscape and exits the page in landscape my list page become all messed up.

I need a way to turn the orientation back to portrait every time the user presses done on the video and returns back to the list.

On the list page I do have

- (BOOL)shouldAutorotate{
  return YES;
}

And I even tried to call shouldAutorotate method in the viewDidAppear but that doesn't work. I know that shouldAutorotate is not being called after the page is loaded so is there a way to check the orientation then flip it or just make it portrait no matter what?

I still need landscape so I am not going to remove it from my plist file.

Any help would be great. Thanks

EDIT

Here is how I call my video player

    MPMoviePlayerViewController *player=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[[main_data objectAtIndex:indexPath.row] objectForKey:@"url"]]];

    UIView * playerView = [player view];
    [playerView setFrame: CGRectMake(0, 0, 480, 320)];

    CGAffineTransform landscapeTransform;
    landscapeTransform = CGAffineTransformMakeRotation(90*M_PI/180.0f);
    landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);

    [playerView setTransform: landscapeTransform];

    [self presentMoviePlayerViewControllerAnimated:player];

    return;    
BigT
  • 1,413
  • 5
  • 24
  • 52
  • Perhaps this question could help you http://stackoverflow.com/questions/2324745/iphone-prevent-application-to-automatically-change-orientation-to-portrait-when?rq=1 – Jack Humphries Mar 21 '13 at 15:00
  • How is the transition from the video list to the video done? Modally, or some other way? – occulus Mar 21 '13 at 15:55
  • Never call shouldAutorotate yourself. It's not a method for you to call; only the system should do that. Calling it yourself won't do anything. – occulus Mar 21 '13 at 15:55
  • I have added how I call the video. I kinda figured I shouldn't call that method but I need it to switch to portrait. – BigT Mar 21 '13 at 17:48
  • 1
    Are you targeting iOS 6? Part of your issue may be that you are using rotation methods from a different iOS version but targeting the newest version, in which rotation has changed. – anon_dev1234 Mar 25 '13 at 16:00
  • I am using `shouldAutorotate` everywhere and that is for iOS 6.0. – BigT Mar 25 '13 at 16:07
  • 2
    `(BOOL)shouldAutorotate` should just return YES or no - you also need to combine it with `(NSInteger)supportedInterfaceOrientations` as Seamus has pointed out below. However, I do believe that your issue lies somewhere else. There is an old method I used once upon a time as follows: `[UIViewController attemptRotationToDeviceOrientation]`. See if that works somewhere when you pop the player from the view stack. – anon_dev1234 Mar 25 '13 at 16:10
  • I tried calling that in my viewDidAppear method. Is that a good place to call this? I have never used that – BigT Mar 25 '13 at 18:46
  • please check this it will surely help you http://stackoverflow.com/questions/5014176/mpmoviewplayercontroller-fullscreen-playback-rotation-with-underlying-uiviewcont – 9to5ios Mar 28 '13 at 13:11
  • Try adding the orientation you want by calling the orientation mask in the same method where you call the video done. Once the video is done then in your movie player method after declaring it then input the view orientation. See if you that helps you out. – Adrian P Mar 31 '13 at 23:38

6 Answers6

2

On iOS 6, the mechanisms for autorotation behavior have changed, so if you're testing on an iOS 6 device, you'll need a little extra work. In your view controller, add the following method:

-(NSInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
Seamus Campbell
  • 17,816
  • 3
  • 52
  • 60
  • I have added this and it does get called when I first view the page. When I go into the video, change the orientation, then go back to that page both the `shouldAutorotate` and `supportedInterfaceOrientations` are not being called. – BigT Mar 25 '13 at 16:10
  • It is because you are popping it from the view stack (modally), and thus the system has no reason to call it – anon_dev1234 Mar 25 '13 at 16:11
  • I checked again and `shouldAutorotate` is not being called the first time (I confused it with another break point). Is there a reason why its not being called in the first place. Is it because the system doesn't know its in the view stack? – BigT Mar 25 '13 at 16:17
  • 1
    How are you presenting your first view controller? Are you using the `window.rootViewController` property, or the older method of just adding the view controller's view as a subview of the window? – Seamus Campbell Mar 25 '13 at 16:24
  • I'm wondering here why the system wouldn't know that your view controller is in the view stack. – Seamus Campbell Mar 25 '13 at 16:24
  • I am adding the view controller view as a subview of the window – BigT Mar 25 '13 at 17:32
  • You should definitely be setting the rootViewController instead. – Seamus Campbell Mar 25 '13 at 17:34
  • Ok so I set it the way you have described. To let ou know I have both a navigation controller and a tab controller. I assume that I am setting the tab controller, which is the bottom tab bar, to the root and the navigation controlller, which is the tab content, to a subview of that – BigT Mar 25 '13 at 17:42
1

You have several approaches available to you. Perhaps the simplest and most controllable is to subclass of MPMoviePlayerViewController.

Start by having your app support ALL interface orientations (in the app-info.plist). Then restrict your list view (let's assume it's MyListViewController.m) to portrait with methods for both iOS 6 and earlier operating systems:

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

- (NSUInteger) supportedInterfaceOrientations {
 return(UIInterfaceOrientationMaskPortrait);
}

- (BOOL) shouldAutorotate {
 return FALSE;   
}

Now create a new Objective C class derived from MPMoviePlayerViewController, called MyMoviePlayerViewController. Here's MyMoviePlayerViewController.h:

#import <MediaPlayer/MediaPlayer.h>

@interface MyMoviePlayerViewController : MPMoviePlayerViewController

@end

And here's MyMoviePlayerViewController.m:

#import "MyMoviePlayerViewController.h"

@interface MyMoviePlayerViewController ()
@end

@implementation MyMoviePlayerViewController

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

- (NSUInteger) supportedInterfaceOrientations {
 return(UIInterfaceOrientationMaskAll);
}

- (BOOL) shouldAutorotate {
 return TRUE;    
}

-(id)initWithContentURL:(NSURL *)contentURL {
 UIGraphicsBeginImageContext(CGSizeMake(1,1));
 self = [super initWithContentURL:contentURL];
 UIGraphicsEndImageContext();
 if (self) {
    self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [[NSNotificationCenter defaultCenter] removeObserver:self
            name:MPMoviePlayerPlaybackDidFinishNotification
            object:[self moviePlayer]];
        [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(movieFinishedCallback:)
            name:MPMoviePlayerPlaybackDidFinishNotification
            object:[self moviePlayer]];
 }
 return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
[[self moviePlayer] setFullscreen:YES animated:NO];
[self moviePlayer].controlStyle = MPMovieControlStyleDefault;
}

- (void)movieFinishedCallback:(NSNotification*)notification {
    MPMoviePlayerController *moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
        name:MPMoviePlayerPlaybackDidFinishNotification
        object:moviePlayer];
    self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

Then, add to MyListViewController.h:

#import "MyMoviePlayerViewController.h"

And, in MyListViewController.m, use:

MyMoviePlayerViewController *player =[[MyMoviePlayerViewController alloc]
    initWithContentURL:URLWithString:[[main_data objectAtIndex:indexPath.row]
    objectForKey:@"url"]]];
[self presentViewController:player animated:YES completion:nil];

Obviously, you can tweak the settings (such as animation styles, controls to display...) to suite your specific needs.

Wildaker
  • 2,533
  • 1
  • 17
  • 19
  • This solution worked best for me. Want to work out some things but overall good solution – BigT Apr 01 '13 at 14:51
0

What about calling [UIViewController attemptRotationToDeviceOrientation] when your video controller is hidden?

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • I tried calling that in my `viewDidAppear` method. Is that a good place to call this? I have never used that – BigT Mar 25 '13 at 16:28
0

Implement this in your list view controller

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return UIInterfaceOrientationPortrait ;
}

-anoop

anoop4real
  • 7,598
  • 4
  • 53
  • 56
0

Try this,

In your viewdidLoad method write this line to support orientation in app,

//rotation
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil];



#pragma mark -Rotation
//method for rotate your playerview, as user changes orientation
-(void)orientationChanged 
{
    NSLog(@"Orientation Changed..!!");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.35];
    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait)  {
        playerView.transform = CGAffineTransformMakeRotation(0);
    }
    else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {
        playerView.transform = CGAffineTransformMakeRotation(M_PI);
    }
    else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)  {
        playerView.transform = CGAffineTransformMakeRotation(M_PI+(M_PI/2));
    }
   else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight)        {
        playerView.transform = CGAffineTransformMakeRotation(M_PI/2);
    }
    [UIView commitAnimations];
}

#ifdef IOS_OLDER_THAN_6
- (BOOL)shouldAutorotateToInterfaceOrientation:   (UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
#endif

// For Newer than IOS 6.
#ifdef IOS_NEWER_OR_EQUAL_TO_6
-(BOOL)shouldAutorotate
{
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskPortrait);
}
#endif

Hope this helps., Thnak you.. happy coding

Dhruvik
  • 984
  • 4
  • 18
0

i think you need to write the code below code in - (void)movieFinishedCallback:(NSNotification*)notification

- (void)movieFinishedCallback:(NSNotification*)notification 
{
    [self.view setFrame: CGRectMake(0, 0, 320, 480)];
    CGAffineTransform landscapeTransform;
    landscapeTransform = CGAffineTransformMakeRotation(270*M_PI/180.0f);
    landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
    [self.view setTransform: landscapeTransform];
}
V.J.
  • 9,492
  • 4
  • 33
  • 49