MPMoviePlayer Controller view should autorotate to landscape right by default - ios

I have two view controllers.One view has button, when user click on the button he will redirect to another view where MPMoviePlayer Controller is present.I want to show MPMoviePlayer controller view to LandsapeRight mode by Default.
I write the below code in buttonAction
-(void)buttonAction
{
tLive = [[toneTvlive alloc]init];
[self.navigationController pushViewController:tLive animated:YES];
}
and in the 2nd view i write the below code
- (void)viewDidLoad
{
[super viewDidLoad];
printf("\n hii");
self.navigationController.navigationBar.hidden = YES;
[[UIApplication sharedApplication]setStatusBarHidden:NO];
NSURL *mediaURL = [NSURL URLWithString:#""];
mp = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
//[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setMovieSourceType:MPMovieSourceTypeStreaming];
[mp setFullscreen:YES animated:YES];
[mp.view setFrame: CGRectMake(0,0, 480,320)];
[self.view addSubview:mp.view];
[mp prepareToPlay];
[mp play];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
while run the program first it shows the view in portrait mode only.It doesn't prompt the view to rotate in landscape,but after when i rotate the device once it works fine.Can anyone please help me to solve this.

If you're using iOS 6+ you can autorotate the view controller to landscape:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate {
return YES;
}
If you're targeting earlier iOS versions, here's a good SO answer about the same thing.

Related

MPMoviePlayerController overlapped by presenting modal viewcontroller

I added a MPMoviePlayerController's view to my view of a viewcontroller, that i present on an iPad with UIModalPresentationFormSheet. This works fine so far, but when i tap the fullscreen button, the movie view changes to fullscreen but after the animation the modal viewcontroller appears above the view.
The only code I use is the following:
- (void)viewDidLoad
{
[super viewDidLoad];
self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[self.player prepareToPlay];
self.player.controlStyle = MPMovieControlStyleEmbedded;
self.player.view.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.player.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullScreen) name:MPMoviePlayerWillEnterFullscreenNotification object:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didEnterFullScreen) name:MPMoviePlayerDidEnterFullscreenNotification object:self.player];
}
- (void) updateViewConstraints
{
[super updateViewConstraints];
if (!self.addedContraints) {
[self.player.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
self.addedContraints = YES;
}
}
This happens when I try to hide the viewcontrollers view.

Can be force MPMoviePlayerController to landscape when click on Fullscreen button in iOS

I create a MPMoviePlayerController in detailView(UIVIew), now i want to force MPMoviePlayerController to landscape view when user click on FullScreen button. Can i do that? Please give me any suggestion. Thanks in advance.And this is my code to create :
NSURL *movieURL = [NSURL URLWithString:previewString];
movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[movieController.view setFrame:CGRectMake(10,130, 275 , 150)];
movieController.view.backgroundColor = [UIColor grayColor];
[detailview addSubview:movieController.view];
[movieController prepareToPlay];
movieController.shouldAutoplay = NO;
and willEnterFullscreen () function:
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
donepress = YES;
// nothing
}
I tried search but still do not have any good answer. Please help me. thanks so much
YES, you can do that with two notification observer to change the full orientation.
First, Add two notification observer to your AppDelegate didFinishLaunchingWithOptions method:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
Second, Add the method and property
- (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
self.allowRotation = YES;
}
- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
self.allowRotation = NO;
}
Third, override the supportedInterfaceOrientationsForWindow method, you can return whatever orientation you want
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskPortrait;
}

MPMoviePlayerViewController in landscape / all orientations

My questions is really simple, tutorials and answers didn't fix my problem.
I have an app with settings:
I want support only Portrait/Upside Down orientations in all my viewControllers except when I want to play video via:
MPMoviePlayerViewController
Here is code:
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[Videos videoURL:video.hash]];
if (mp) {
isVideoPlaying = YES;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(videoFinishedPlaying:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mp.moviePlayer];
[self presentMoviePlayerViewControllerAnimated:mp];
mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[mp.moviePlayer play];
[mp release];
}
When MPMoviePlayerViewController plays video I want support all orientations.
Need your help.
You should permit rotation by the following:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
}
IOS 6:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
put the code in the .m file where you call the player
Hi all I had same problem I resolved it -
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

MPMOVIEPLAYER when playing a video multple times in Landscape causes video to shift down and navigation bar disappears

I am writing an application that has imbedded videos. If I start the application in Landscape mode first and then start the video it looks good. So then I click on Done and then play the video a second time. The second time the video is now shifted down and to the left. When I click on the video the Done button does not show up. I have to switch the simulator to Portrait mode and then back to Landscape mode and then I can click on Done to stop the video. If I always start the video in Portrait mode, and then turn the simulator to landscape it will work. I can do this many times and the video will always line up properly. However if I play the application always in landscape mode, the video will always shift down and to the left for every play after the first time. I went through many of the other mpmovieplayer questions and compared my code to the other code. I am using IOS6 and Storyboard. I have copied my basic MPMOVIEPLAYER code below. I am really stuck here
MoviePlayerViewController.m
- (void) readyPlayer
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
// For 3.2 devices and above
if ([mp respondsToSelector:#selector(loadState)])
{
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
// Register to receive a notification when the movie has exit fullscreen mode.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
}
/*---------------------------------------------------------------------------
* For 3.2 and 4.x devices
*
*--------------------------------------------------------------------------*/
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
{
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
// When tapping movie, status bar will appear, it shows up
// in portrait mode by default. Set orientation to `landscape`
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
// Set frame of movie player
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
} else {
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 1024, 748)];
[[self view] setCenter:CGPointMake(374, 512)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
// Set frame of movie player
[[mp view] setFrame:CGRectMake(0.0, 0.0, 1024.0, 748.0)];
}
// Add movie player as subview
[[self view] addSubview:[mp view]];
// Play the movie
[mp play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}

Setting MPMoviePlayerController orientation without MPMoviePlayerViewController

It seems the best (only?) way to rotate an MPMoviePlayerController to landscape is to use an MPMoviePlayerViewController .
I'm currently modifying an open source game which, unfortunately, does not have a root UIViewController set. Is there any way to set the orientation only with MPMoviePlayerController, or do I have to suck it up and hack a root UIViewController into this application?
Edit: in response to Shivan Raptor, the game is Canabalt: https://github.com/ericjohnson/canabalt-ios. I'm instantiating an MPMoviePlayerController as follows:
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:self.videoTempFilePath];
self.moviePlayerController.repeatMode = MPMovieRepeatModeNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerDidExitFullscreenNotification
object:self.moviePlayerController];
[glView addSubview:self.moviePlayerController.view];
// MUST SET THIS HERE AFTER WE ADD AS SUBVIEW
self.moviePlayerController.fullscreen = YES;
[self.moviePlayerController play];
The movie plays in portrait and stays locked there, but I'd like to play it in landscape.
I created a root UIViewController and made the MPMoviePlayerViewController a child of it. This created the desired effect.
I think this might helpful.
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
return true;
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI * 2)];
return true;
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
[self.view setTransform:CGAffineTransformIdentity];
return true;
}
else return false;
}

Resources