3

I want to view MPMoviePlayerController fullscreen. It can be a horizontal view. MPMoviePlayerController can only in portrait view. This is my code tried:

Create MPMoviePlayerController:

    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;

    [detailview addSubview:movieController.view];

I tried above code but when moviecontroller is fullscreen, it can only view in portrait mode. It cannot be viewed in landscape mode. I only want moviecontroller in fullscreen mode can view in landscape mode without another view still view in portrait mode. What should I do?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Joson Daniel
  • 409
  • 1
  • 9
  • 18

4 Answers4

4

in your .h file

@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;

and in your .m file synthesize that property

@synthesize moviePlayer = _moviePlayer;

I have movie file in my resource folder, change your name accordingly:

            _moviePlayer =  [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video001" ofType:@"MOV"]]];
           [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(moviePlayBackDidFinish:)
                                                         name:MPMoviePlayerPlaybackDidFinishNotification
                                                       object:_moviePlayer];
            _moviePlayer.controlStyle = MPMovieControlStyleDefault;
            _moviePlayer.shouldAutoplay = YES;
            [_moviePlayer prepareToPlay];
            [self.view addSubview:_moviePlayer.view];
            [_moviePlayer setFullscreen:YES animated:YES];
            [_moviePlayer stop];
            [_moviePlayer play];

and for closing the fullScreen I have method like this:

- (void) moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];

    if ([player
         respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [player.view removeFromSuperview];
    }
}

Hope it works for You.

Ravi_Parmar
  • 12,319
  • 3
  • 25
  • 36
4

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;
}
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Slemon
  • 791
  • 7
  • 12
  • I've tried this very same approach in Swift, but the application still shows the video in portrait... Does anyone know if it works in Swift? – F3RD3F Jan 21 '15 at 11:31
  • @F.D.FDev Ran into the same problem. Have you found a way to play video in full-screen landscape mode even if the user locked the screen rotation? – JSNoob Nov 12 '15 at 03:56
1

I have used the following code in one of my apps in landscape mode .

    [moviePlayerController.view setFrame:CGRectMake(0, 0, 1024, 768)];
    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    moviePlayerController.repeatMode = MPMovieRepeatModeOne;
    moviePlayerController.controlStyle = MPMovieControlStyleNone;
    [moviePlayerController play];
SRI
  • 1,514
  • 21
  • 39
  • No, my problem is movieController can be view in FULLScreen mode but when view in Fullscreen mode, it can not rotate in landscape.Thanks – Joson Daniel Sep 13 '13 at 02:40
  • Thanks but it can still not work. when i click on FullScreen button, movieplayer view in FullScreen mode but when i rotate iphone/Simulator, movieplayer can not rotate – Joson Daniel Sep 13 '13 at 02:49
  • It seems not works. Can you give me another suggesstion ? Thanks – Joson Daniel Sep 13 '13 at 03:03
1

The challenege with your approach is that by simply adding a subview to your view, handling rotation events are your responsibility.

Sounds like you should be using the MPMoviePlayerViewController instead:

MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];

[self presentMoviePlayerViewControllerAnimated:playerController];

[playerController.moviePlayer play];

Provided that you have your orientation options set up correctly in your app, rotation will be handled by the player controller and will play both portrait and landscape content. If your app doesn't currently support landscape, the movie player controller won't either.

ChrisH
  • 4,468
  • 2
  • 33
  • 42