MPMoviePlayerController scaling mode issues after exit full screen

| | August 5, 2015

i am just playing a video by using MPMoviePlayerController…my code is

-(void)playMovie:(NSURL *)url
{
    moviePlayer =
    [[MPMoviePlayerController alloc]
     initWithContentURL:url];
    if (IDIOM==IPAD) {
        [moviePlayer.view setFrame:CGRectMake(22,100, 720, 300)];
    }
    else
    {
        (IS_IPHONE_5)? [moviePlayer.view setFrame:CGRectMake(22, 70, 280, 150)]:[moviePlayer.view setFrame:CGRectMake(22, 40, 260, 140)];
    }
    [_scrollView addSubview:moviePlayer.view];
    moviePlayer.scalingMode =MPMovieScalingModeFill;
    [moviePlayer prepareToPlay];
    [moviePlayer play];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidEnterFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidExitFullScreen:) name:MPMoviePlayerDidExitFullscreenNotification object:Nil];

}

-(void)moviePlayerDidEnterFullscreen :(id)sender
{
    NSLog(@"fullscreen");
   [moviePlayer play];
    moviePlayer.scalingMode =MPMovieScalingModeFill;

}

- (void) moviePlayerDidExitFullScreen:(id)sender {

    NSLog(@"exit full screen");
    [moviePlayer play];
    moviePlayer.scalingMode =MPMovieScalingModeFill;

}

here when i play initially video will be in “MPMovieScalingModeFill” mode…but my problem is that if i press full screen it shows video on full screen ..when i press exit “full screen” then my video mode goes to “MPMovieScalingModeAspectFit” mode.but i need to be always in “MPMovieScalingModeFill” mode .whats wrong with my code..Please help me…

3 Responses to “MPMoviePlayerController scaling mode issues after exit full screen”

  1. First set the ScalingMode to None and then set the ScalingMode to AspectFill

    Swift Code :

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayerExitFullscreen:", name: MPMoviePlayerDidExitFullscreenNotification, object: self.moviePlayer)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayerEnterFullscreen:", name: MPMoviePlayerWillEnterFullscreenNotification, object: self.moviePlayer)
    
    func moviePlayerEnterFullscreen (notification : NSNotification)
    {
        self.videoPlayer.moviePlayer.scalingMode = MPMovieScalingMode.None
        self.videoPlayer.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill
    }
    
    func moviePlayerExitFullscreen (notification : NSNotification)
    {
        self.videoPlayer.moviePlayer.scalingMode = MPMovieScalingMode.None
        self.videoPlayer.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill
    }
    
  2. This isn’t the “ideal” solution, but it works! Basically, once you exit full screen the MPMoviePlayerController instance gets all screwed up and resetting the scaling property to MPMovieScalingModeFill won’t help no matter where or when you do it (I’ve tried all sorts of stuff and after an hour gave up). Easiest solution is to remove the MPMoviePlayerController and simply allocate a new instance of MPMoviePlayerController each time full screen is exited (not ideal, but totally works):

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:NO];
        if (self.moviePlayer != nil)
            [self.moviePlayer.view removeFromSuperview];
        self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL];
        self.moviePlayer.view.frame = CGRectMake(#, #, #, #);
        self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
        self.moviePlayer.shouldAutoplay = NO;
        [self.moviePlayer setContentURL:self.videoURL];
        [self.moviePlayer prepareToPlay];
        [self.moviePlayer setScalingMode:MPMovieScalingModeFill];
        [self.view addSubview:self.moviePlayer.view];
    }
    

    PS: Don’t forget to call super on viewDidAppear or suffer all sorts of unforeseeable mayhem (a very common mistake in iOS development)

  3. I believe this will generate the MPMoviePlayerScalingModeDidChangeNotification.

    [[NSNotificationCenter defaultCenter] addObserver:self 
                    selector:@selector(movieScalingModeDidChange:) 
                    name:MPMoviePlayerScalingModeDidChangeNotification 
                    object:nil];
    

    Source :Apple Doc

    MPMoviePlayerScalingModeDidChangeNotification

    Posted when the scaling mode of a movie player has changed. There is no userInfo dictionary.
    Scaling mode can change programmatically or by user interaction. To set or retrieve the scaling mode of a movie player, access its scalingMode property. The movie player whose state has changed is available as the object associated with the notification.

Leave a Reply