Question

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

Was it helpful?

Solution

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
    }

}

OTHER TIPS

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];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top