MPMoviePlayerController fullscreen quirk in iPad

后端 未结 7 1651
無奈伤痛
無奈伤痛 2020-12-23 23:22

I want to show a MPMoviePlayerController in a view controller and let the user toggle full screen with the default controls, like the YouTube app. I\'m using the following c

相关标签:
7条回答
  • 2020-12-23 23:47

    Had the same problem, just spent half a day sorting it out. With the iPad in portrait orientation, whenever I started a video using the sample code (or any I could find on the net) the video and control bar were formatted for portrait, and hence all over the place on the screen.

    Anyway, the following works for me.

      /* Call the code like below:
            int iLandscape;
            if( newOrientation==UIInterfaceOrientationLandscapeLeft || newOrientation==UIInterfaceOrientationLandscapeRight )
                 iLandscape=1;
    
            [self PlayVideo:iLandscape fullscreen:1]
        */
            //////////////////////////////////////////////////////////////////////////
            - (void)PlayVideo:(int)iLandscape fullscreen:(int)iFullScreen 
            {
                NSString *url = [[NSBundle mainBundle] pathForResource:@"myvideofile" ofType:@"m4v"];
    
            if( iFullScreen==0 )
            {
                MPMoviePlayerController *player2 = 
                    [[MPMoviePlayerController alloc] 
                        initWithContentURL:[NSURL fileURLWithPath:url]];
    
                [[NSNotificationCenter defaultCenter] 
                    addObserver:self
                       selector:@selector(movieFinishedCallback:)
                           name:MPMoviePlayerPlaybackDidFinishNotification
                         object:player2];
    
                //---play partial screen---
                player2.view.frame = CGRectMake(0, 0, m_iScreenWidth, m_iScreenHeight);
                [self addSubview:player2.view];
                //---play movie---
                [player2 play];
            }   
            else
            {
                MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] 
                    initWithContentURL:[NSURL fileURLWithPath:url]];
    
                [[NSNotificationCenter defaultCenter] 
                    addObserver:self
                       selector:@selector(movieFinishedCallback:)
                           name:MPMoviePlayerPlaybackDidFinishNotification
                         object:[playerViewController moviePlayer]];
    
                if( iLandscape )
                {
                    playerViewController.view.frame = CGRectMake(0, 0, m_iScreenWidth, m_iScreenHeight);
                }
                [self addSubview:playerViewController.view];
                //play movie
                MPMoviePlayerController *player = [playerViewController moviePlayer];
                player.scalingMode=MPMovieScalingModeAspectFit;
                [player play];
            }
        }
    
    
        //////////////////////////////////////////////////////////////////////////
        - (void) movieFinishedCallback:(NSNotification*) aNotification 
        {
            MPMoviePlayerController *player = [aNotification object];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];    
            [player autorelease];    
            [player.view removeFromSuperview];
        }
    
    0 讨论(0)
  • 2020-12-23 23:50

    are you using interface builder for your UI? if so make sure you set the view's orientation to 'landscape' in the view attributes inspector.

    0 讨论(0)
  • 2020-12-23 23:57

    Did you solve this issue?

    [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];
    

    This code might help you.

    0 讨论(0)
  • 2020-12-24 00:01

    Yeah, I'm experiencing this problem as well. It definitely appears to be a bug in the MPMoviePlayerController itself.

    The workaround I've settled on in my application is to just correct the status bar myself when I exit fullscreen mode:

    - (void)playerDidExitFullscreen:(NSNotification *)notification {
        MPMoviePlayerController *moviePlayer = (MPMoviePlayerController *) notification.object;
    
        if (moviePlayer == self.player) {
            UIApplication *app = [UIApplication sharedApplication];
            if (app.statusBarOrientation != self.interfaceOrientation) {
                [app setStatusBarOrientation:self.interfaceOrientation animated:NO];
            }
        }
    }
    

    This doesn't fix the problem while in fullscreen mode, but it does fix it afterwards.

    Note of course that the function needs to be added to the notification:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    
    0 讨论(0)
  • 2020-12-24 00:06

    Try this

     - (void)willEnterFullscreen:(NSNotification*)notification {
         NSLog(@"willEnterFullscreen");
    
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
      }
    
     - (void)enteredFullscreen:(NSNotification*)notification {
         NSLog(@"enteredFullscreen");
     }
    
    - (void)willExitFullscreen:(NSNotification*)notification {
        NSLog(@"willExitFullscreen");
    
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    
    
    }
    
    - (void)exitedFullscreen:(NSNotification*)notification {
        NSLog(@"exitedFullscreen");
    
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    0 讨论(0)
  • 2020-12-24 00:07

    Is shouldAutorotateToInterfaceOrientation:interfaceOrientation returning YES for all of the supported orientations?

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
    }
    

    If you provided more of your code it would help.

    0 讨论(0)
提交回复
热议问题