IPhone App: MPMoviePlayer: PlayVideo in landscape mode only

Hi I have an iPhone app that only works in portrait mode. But I want mpmovieplayer to only play videos in landscape mode

How can I achieve this?

here is the code.

    NSString *path = [[NSBundle mainBundle] pathForResource:lblVideoName.text ofType:@"mp4" inDirectory:nil];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
    NSLog(@"URL== %@",url);
    moviePlayer =  [[MPMoviePlayerController alloc]
                    initWithContentURL:url];


    moviePlayer.controlStyle = MPMovieControlStyleDefault;
    moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:moviePlayer.view];
    [moviePlayer setFullscreen:YES animated:YES];

      

Please help and suggest.

thank

+3


source to share


1 answer


You can present the movie in your own view controller, which is set up for the landscape.

// in the VC where the user indicates he wants to see a movie...
- (void)startTheMovie {
    // run a storyboard segue with a modal transition, or ...
    MyMovieVC *movieVC = [[MyMovieVC alloc] initWithNibName:@"MyMovieVC" bundle:nil];
    [self presentModalViewController:movieVC animated:YES];
}

// in MyMovieVC

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
        (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

// and the code that you wrote on view will appear

      

You can enable the dismiss button in this interface, or generally you must dismiss it. You can do this by subscribing to a ready-made notification:

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(moviePlayerFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

      



then according to the ready message

- (void)moviePlayerFinished:(NSNotification*)notification {
    [self dismissModalViewControllerAnimated:YES];
}

      

Please note that if you do this in a tabbed interface, all tabs - even those that are not visible - must agree for the interface to change. It makes some sense, but it gave me heartache in the past. I have no good solution. My approach was public BOOL isLandscapeOK on my AppDelegate. This MovieVC would set it to YES and the other VC tabs would respond to portrait or landscape if isLandscapeOK == YES.

+1


source







All Articles