Reputation: 906
I'm having some strange behavior where my MPMoviePlayerViewController
isn't auto
-rotating when the orientation
changes. However, I recreated the same view hierarchy in a fresh project and when the MPMoviePlayerViewController
player was up, it rotated to every orientation. I've scoured the project looking for anything that might be setting the orientation explicitly, but there is nothing.
I'll lay out all the relevant information here and the things that I've tried so far.
The view hierarchy currently looks like this:
Every class in the view hierarchy responds to shouldAutorotateToInterfaceOrientation with YES only for UIInterfaceOrientationPortrait.
Things I've tried:
shouldAutorotateToInterfaceOrientation
up stack from the "Root" VC up to the MPMoviePlayerViewController
MPMoviePlayerViewController
subclass' implementation of shouldAutorotateToInterfaceOrientation
to return YES for both landscape orientations and YES for all orientations.presentMoviePlayerViewControllerAnimated
from other VCs like the Feed VCIf the movie player properly rotates in a fresh project with same view hierarchy, what could possible be getting in the way here. Any ideas as to where the orientation might be getting stuck?
Upvotes: 22
Views: 8062
Reputation: 57
Add this method in AppDelegate.m
-(NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if(!ISIPAD)
{
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]] && ![[self.window.rootViewController presentedViewController] isBeingDismissed])
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
return UIInterfaceOrientationMaskPortrait;
}
}
return UIInterfaceOrientationMaskAllButUpsideDown ;
}
Upvotes: 0
Reputation: 859
I had lot of pain with MPMoviePlayerViewController, which should be only controller able to rotate from portrait to landscape and vice versa. It was iOS7, iOS8 application with storyboard.
Here is solution:
Every UIViewController that need to support just portrait mode, should implement next methods, like this
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
MPMoviePlayerViewController should be extended and next methods should be overriden like this
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
Use presentMoviePlayerViewControllerAnimated
to display MPMoviePlayerViewController
Upvotes: 0
Reputation: 1247
this guide helped me checking some steps I was missing in order to allow only the video player to be viewed in landscape, leaving the rest of the application to be fixed in portrait mode:
Upvotes: 0
Reputation: 9877
You should try this (Worked for me):
Declare in .h file:
BOOL landscape;
.m file:
-(IBAction)PlayMovie:(NSString *)movieName {
landscape = YES;
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:movieName ofType:@"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
-(void)dismissMoviePlayerViewControllerAnimated {
landscape = NO;
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// You can change the return for your needs.
if (landscape == YES) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else { return NO; }
}
What I did here is Creating my movie view and set the "landscape" BOOL to YES.
Then the "shouldAutorotateToInterfaceOrientation" will detect this and autorotate your view.
And when the movie is finished I set the "landscape" to NO so that the view rotates back.
Upvotes: 0
Reputation: 86317
I had a similar problem.
Solved by:
now your app will start rotating in all Orientations, if you dont want this, then skip step 1, only add following method in appDelegate
removing shouldAutorotateToInterfaceOrientation everywhere
adding following method to view controller for supporting Orientations u needed for your app
you can add the same method as in step 4 in MPMoviePlayerViewController's subclass for whatever Orientations u needed for video player
Upvotes: 0
Reputation: 906
Solution:
For anyone that might encounter this, the reason the video wasn't rotating was that I was accidentally adding the RootViewController has the window's rootViewController, rather than the UINavigationController.
self.window.rootViewController = navController;
is correct
self.window.rootViewController = rootViewController;
is not
Thank you guys for your help and input along the way.
Upvotes: 1
Reputation: 9926
A few ideas:
UINavigationController
set as the rootViewController
property of your app's UIWindow
? You didn't mention that. You should do this rather then adding the navigation controller's view to the window.UINavigationController
one after another, with animated:YES
. You might have a similar issue.Upvotes: 1
Reputation: 1932
there are some reasons behind at your query..
***You are calling MPMoviePlayerViewController
.. so apply AutoOrientation on "Feed" View Controller
And try to call by PushViewController..
***Use MPMoviePlayerController
instead of MPMoviePlayerViewController
and add subview in to FeedViewController
..
Sample code for MPMoviePlayerController--
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"type"]];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
videoPlayer.controlStyle = MPMovieControlStyleNone;
videoPlayer.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.height, self.view.frame.size.width);
[videoPlayer prepareToPlay];
videoPlayer.view.center = self.view.center;
videoPlayer.fullscreen = YES;
[self.view addSubview:videoPlayer.view];
[videoPlayer play];
***Check your Xcode target setting and apply enable All orientation..
Upvotes: 0
Reputation: 7663
I know this might be a stupid suggestion but make sure that the shouldAutorotateToInterfaceOrientation
method in your MPMoviePlayerViewController
subclass is getting called. Maybe something went wrong there...
Also make sure that you don't have 2 subviews added to the main UIWindow
as specified here:
Q: Why won't my UIViewController rotate with the device?
[...]
- The view controller's UIView property is embedded inside UIWindow but alongside an additional view controller.
I think that might give you some problems too. You can find more information about what could go wrong in the link above.
Upvotes: 2
Reputation: 319
I will suggest you not to use presentMoviePlayerViewControllerAnimated
, rather than add as subview. I think it will fix your problem nicely.
MPMoviePlayerViewController *mpviewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:self.filePath]];
[self.view addSubview:mpviewController.view];
[self setWantsFullScreenLayout:YES];
And remove the mpviewController.view
when MPMoviePlayerPlaybackDidFinishNotification
detected. Let me see your success...
Upvotes: 7
Reputation: 166
Are you using storyboards? Compare the orientation settings for your UIViewControllers and your UINavigationController between your broken project and your test project. The "Orientation" setting on the attributes inspector may be locking you into one orientation.
You mentioned shouldAutorotateToInterfaceOrientation:
and your plist settings so I won't get into that...
Upvotes: 0
Reputation: 10655
I've found that MPMoviePlayerViewController
objects will honor the project's Info.plist settings for supported interface orientations. In a project of mine I was only allowing landscape views in that file so the movie player will not rotate, even when it answered YES
to landscape orientations in shouldAutorotateToInterfaceOrientation:
.
Edit: OK, grasping at straws: Do you implement automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers
in any of your UIViewController
subclasses? If so, and it returns NO
, your subclasses must forward the appropriate methods to any child controllers upon orientation change.
Otherwise is there any way to see your code?
Upvotes: 3