Forcing landscape orientation on fullscreen MPMoviePlayerController prevents correct rotation when exiting fullscreen
Solution 1
Hi all I had same problem I resolved it -
Here is my complete code....
You need to first change in appdelegate:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[[NowPlaying sharedManager] playerViewController] allowRotation])//Place your condition here
{
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
Register Notifications for the full screen control:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:)
name:MPMoviePlayerWillEnterFullscreenNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:)
name:MPMoviePlayerWillExitFullscreenNotification
object:nil];
Then add line of code in the player controller:
- (void)moviePlayerWillEnterFullscreenNotification:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^
{
self.allowRotation = YES;
});
}
- (void)moviePlayerWillExitFullscreenNotification:(NSNotification *)notification
{
self.allowRotation = NO;
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];
dispatch_async(dispatch_get_main_queue(), ^
{
//Managing GUI in pause condition
if (self.currentContent.contentType == TypeVideo && self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused)
{
[self.moviePlayerController pause];
if (self.playButton.selected)
self.playButton.selected = NO;
}
self.view.transform = CGAffineTransformMakeRotation(0);
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
self.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
});
}
This code is tested in iOS6 and iOS7 working fine. Thanks :)
Please let me know if there is any question.....
Solution 2
You need to subclass and provide landscape as supported interface orientation.
@interface MyMovieViewController : MPMoviePlayerViewController
@end
@implementation MyMovieViewController
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
@end
Solution 3
You could try to "force" an orientation refresh to let the system handle the correct orientation for you:
- (void)forceOrientationRefresh
{
// Force orientation refresh
[self presentModalViewController:[UIViewController new]
animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
It's hack-ish but it works.
Related videos on Youtube
svth
Updated on June 13, 2022Comments
-
svth about 2 years
I have an iPhone application (iOS6+) that supports all interface orientations. However, only landscape orientation should be supported when an MPMoviePlayerController is playing a video fullscreen.
I found the following solution on Stack Overflow and it works.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
...
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (self.landscapeOnlyOrientation) { return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; } return UIInterfaceOrientationMaskAll; } - (void)moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification { self.landscapeOnlyOrientation = YES; } - (void)moviePlayerWillExitFullscreenNotification:(NSNotification*)notification { self.landscapeOnlyOrientation = NO; }
However, an annoying problem persists, namely that if the video exits fullscreen in portrait orientation (after playing in forced landscape), the underlying view doesn't rotate back. I have to manually rotate the device to landscape and back to portrait to trigger updating of the orientation. Is there some way I can trigger this kind of update programatically?
The following set of screenshots should illustrate what I mean:
NB: For various reasons, using MPMoviePlayerViewController is not possible.
-
matt over 10 yearsI submitted a bug to Apple on this issue months ago. I suggest you do the same. The problem is that the orientation methods of the underlying view controller are not being consulted.
-
-
karthikeyan about 10 yearswhat condition i have to check here?if ([[[NowPlaying sharedManager] playerViewController] allowRotation])//Place your condition here and what is mean by now playing?
-
Satya over 9 yearsHi Vijay, thanks for the code. it is working fine after removing the dispatch_async code in the WillExitFullScreen method. Can you brief me why you put the code which is not useful and what is TypeVideo??
-
Vijay Sharma over 9 yearsThanks Satya for comment. I used dispatch_async for accessing main thread because in my case when I am entering into landscape mode my previous GUI is getting distorted while animating. In my case I am playing audio and video on same player and whenever a video start playing it gets full screen not in audio song. So I used TypeVideo for video songs.
-
Vijay Sharma over 9 yearsSorry karthikeyan for late reply. [[[NowPlaying sharedManager] playerViewController] allowRotation] In my case PlayerViewController get Landscape in case I am entering in full screen player mode. So at the time of -> '- (void)moviePlayerWillEnterFullscreenNotification' notification method calling on playerview controller, I am saying that yes allow all rotation now by 'self.allowRotation = YES;' Please let me know if you are not clear on this. Thanks.