I want to show an MPMoviePlayer in landscape mode on launch of the application. Now it starts in portrait mode. There are some codes which forces application to launch in landscape mode. But it is said that these code segments belong to private api so app store will not accept the application. Since morning I am trying to find a way, but no result... Can anyone help me?
This is where I am:
NSURL *url = [[NSURL alloc] initWithString:urlString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayerPlaybackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
[self setWantsFullScreenLayout:YES];
[moviePlayer prepareToPlay];
//For viewing partially.....
moviePlayer.view.backgroundColor = [UIColor blackColor];
//[moviePlayer.view setFrame:CGRectMake(0, 0, 320, 410)];
[moviePlayer.view setFrame:[self.view bounds]];
moviePlayer.fullscreen = YES;
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[self.view addSubview:moviePlayer.view];
//[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
[moviePlayer play];
Thank in advance..
Open your project's info.plist under the Resource group in xCode.
select last line and click on the +(plus) icon. Now select the key 'Initial interface orientation' for value 'Landscape (left home button)'.
or you can select any value from the list shown on clicking drop down buttons.
If all your app is gonna be in landscape you can change it at your project's plist. If not, you can also subclass MPMoviePlayerControllerView and implement and do something like this.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation {
// Return YES for supported orientations.
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
return NO;
}
Use MPMoviePlayerViewController instead of MPMoviePlayerController. It will handle orientation, control style etc. I referred https://stackoverflow.com/a/4911172/3346048 for the same.
MPMoviePlayerViewController *playerView = [[[MPMoviePlayerViewController alloc] initWithContentURL:videoURL] autorelease];
[self presentMoviePlayerViewControllerAnimated:playerView];
and in the AppDelegate.m declare the following function. Which will restrict the orientation to portrait in all cases and will only change when a video is playing:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return return UIInterfaceOrientationMaskLandscape;
}
}
Related
i had spend 2 hours finding the correct code to fix my orientation problem
this are my movieplayer code. i need this particular view to be shown in landscape.
in appdelegate i set all orientation and in my rootviewcontroller i set is as portrait only and in my movieplayer view as landscape but not luck. can anybody give me some comment on how to fix the issues please ?
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskAll;
}
my rootviewcontroller
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(void)prepareIntroVideo
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"designthinking_pt1" ofType:#"mp4"]];
self.playercontroller = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.playercontroller.view setFrame:CGRectMake(0, -20, 320, 480)];
self.playercontroller.movieSourceType = MPMovieSourceTypeFile;
self.playercontroller.scalingMode = MPMovieScalingModeAspectFill;
self.playercontroller.fullscreen = NO;
self.playercontroller.controlStyle = MPMovieControlStyleFullscreen;
//playercontroller.controlStyle = MPMovieControlStyleFullscreen;
self.playercontroller.view.userInteractionEnabled =YES;
self.playercontroller.view.backgroundColor = [UIColor blackColor];
self.playercontroller.shouldAutoplay = NO;
//playercontroller.repeatMode = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playercontroller];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground)
name: UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:[UIApplication sharedApplication]];
[self.playercontroller prepareToPlay];
[self.view addSubview:self.playercontroller.view];
[self.playercontroller setFullscreen:YES animated:YES];
//[self.playercontroller stop];
[self.view sendSubviewToBack:self.playercontroller.view];
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationLandscapeLeft;
}
You can check my example... https://dl.dropboxusercontent.com/u/19438780/testRotate.zip
I have 2 controllers with navigation controller - portrait; for this I "subclassed" nav controller (here is the "rotation" code for portrait)
The 3th controller is landscape - must be presented modal if you want to rotate it - put the rotation code accordingly .
I would look into preferredInterfaceOrientationForPresentation, I spent about a week on this problem a few months ago. I don't remember exactly how i solved it but I do know it had something to do with implementing that method
My code was working fine until I upgraded iPhone to iOS 5.0. The MPMoviePlayerViewController used to work fine but it doesn't work on iOS 5.0 so I have to use MPMoviePlayerController for iOs 5.0 and later versions. It works fine but MPMoviePlayerController doesn't rotate automatically like it used to do with MPMoviePlayerViewController.
Following is my code. Could anyone please suggest me how to make MPMoviePlayerController code rotate automatically?
-(void)playVideo {
NSString *filePath = [appDelegate filePath:#"startup.mp4"];
if(!appDelegate.iOS5) {
// This works perfectly till iOS 4 versions. Rotates automatically
MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:filePath]] autorelease];
[self presentMoviePlayerViewControllerAnimated:videoController];
} else {
// This doesn't rotate automatically
NSURL *url = [NSURL fileURLWithPath:filePath];
MPMoviePlayerController* moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
Try subclassing MPMoviePlayerController and forcing the orientation to portrait only.
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Not the best solution but I guess it should work.
I am creating a application in which I can need to play a video which I do by mpmovieplayer controller .Now i nee to do this for both orientation .But the frame doesnt get set properly .
The code is as follws
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self shouldAutorotateToInterfaceOrientation:[UIDevice currentDevice].orientation];
NSURL *temp = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Floating" ofType:#"mp4"]];
mpviewController = [[MPMoviePlayerViewController alloc] initWithContentURL:temp];
mpviewController.view.frame = CGRectMake(0, 0, 768, 1024);
mpviewController.view.backgroundColor = [UIColor clearColor];
mpviewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mpviewController.view.userInteractionEnabled= NO;
mpviewController.moviePlayer.fullscreen= YES;
mpviewController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[mpviewController moviePlayer] play];
[self.view addSubview:mpviewController.view];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
currentOrientation = interfaceOrientation;
//[self SetInterface];
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
mpviewController.view.frame = CGRectMake(0, 0, 768, 1024);
else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
mpviewController.view.frame = CGRectMake(0, 0, 1024, 768);
return YES;
}
I dont know where I am wrong.Please let me know for any chages to make in code. So as to get proper orientation.
Ragards Abhi
First
I believe you don't need to resize the mpviewController as it will resize it self alone.
you should only set the -
Second
In the shouldAutorotateToInterfaceOrientation you only set the supported directions in shouldAutorotateToInterfaceOrientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
If it dose not do it you change the view properties in -
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)){
//do what you want for portrait
}else{
//do what you want for landscape
}
}
you should only return either YES or NO in shouldAutorotateToInterfaceOrientation: method, it's called by framework only to get the information about the supported orientation in your controller, Read the apple documentation for the same.
you need to register for the orientation change notifictaion
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
Implement your orientationChanged: method.
//********** ORIENTATION CHANGED **********
- (void) orientationChanged:(NSNotification *)note
{
NSLog(#"Orientation has changed: %d", [[note object] orientation]);
//Put your code here from shouldAutorotateToInterfaceOrientation:
}
Not forget it to remove .
[[NSNotificationCenter defaultCenter] removeObserver:self];
Here are some link
Device orientation - autorotate?
Orientation Changed Notification
No need to change any codings .. simple insert the following codings to the application , it will automatically detect the orientation...
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor blackColor]];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"sharkdivertrailer" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(184, 200, 400, 300);
[theMovie play];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
So I have an alert view pop up in my application and on clicking a view button a video is supposed to play but nothing happens in the simulator. I don't want to test this on a device until I get it working in the simulator. This is the code below as far as I can tell it should work. It reaches the Log statement and outputs to the console but the video does not play. Any ideas?
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex{
if (buttonIndex == 1) {
NSURL *url = [NSURL URLWithString:#"http://srowley.co.uk/endyVid.mp4"];
MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease];
//---play movie---
[player setFullscreen:YES];
[self.view addSubview:[player view]];
NSLog(#"Player Should play!");
[player play];
}
}
You need to set the frame on player's view.
[[player view] setFrame: [self.view bounds]];
Now I think I know about the differences between 3.X and 4 with regards the MPMoviePlaybackController and the need to set the view and have it fully working in a child view controller. But despite the following code seeming correct (to me) I still just get a blank screen for the duration of the movie. I know it plays successfully as moviePlayBackDidFinish fires.
Do I need to add it to a modal or similar at this point?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
player.fullscreen = YES;
player.controlStyle = MPMovieControlStyleNone;
[[player view] setFrame:window.bounds];
[window addSubview: [player view]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
}
the MPMoviePlayerController does NOT have a view property.
you should/must use MPMoviePlayerViewController instead.
here's what I do:
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:currentChannel.StreamURI]];
[moviePlayerViewController.moviePlayer setControlStyle:MPMovieControlStyleNone];
moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
moviePlayerViewController.moviePlayer.shouldAutoplay = YES;
[moviePlayerViewController.moviePlayer setScalingMode: MPMovieScalingModeAspectFit];
moviePlayerViewController.view.frame = CGRectMake(0, 0, 480, 320);
[self.view addSubview:moviePlayerViewController.view];
note that you might want to change movieSourceType to MPMovieSourceTypeFile or MPMovieSourceTypeUnknown.
The above code is 100% of what I need to play a Movie (in my case a streaming channel)
In my case I had moved the
[window makeKeyAndVisible];
Out from
didFinishLaunchingWithOptions
and into my
movieDidFinish
By putting it back it worked