3

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...

jafar
  • 51
  • 1
  • 5
  • You should not trigger `play` within the fullscreen-notifications. – Till Jan 09 '14 at 15:45
  • but it stop playing so i called the play again within the full screen notification – jafar Jan 09 '14 at 18:15
  • That makes no sense at all. It should not stop playing when changing from or to fullscreen. There must be something within your code that you did not show us that does stop the playback. My guess is, you got something within your "viewWill/DidAppear"/"viewWill/DidDisappear" - those appearance notifications are triggered when leaving or entering fullscreen as the fullscreen mode entirely hides your viewController's view. – Till Jan 09 '14 at 19:28
  • yeah i did it in DidDisappear for stoping the player when i leave the controller,is any other better way for stoping the player when i leave the controller? – jafar Jan 09 '14 at 21:41
  • A common way is to check for `MPMoviePlayerController`'s `fullscreen` property within your `viewDidDisappear`. If that `fullscreen` is active, do not call `stop`. – Till Jan 09 '14 at 21:59

3 Answers3

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.

codercat
  • 22,873
  • 9
  • 61
  • 85
3

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.moviePlayer.scalingMode = MPMovieScalingMode.None
    self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill
}

func moviePlayerExitFullscreen (notification : NSNotification)
{
    self.moviePlayer.scalingMode = MPMovieScalingMode.None
    self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill
}
Leojin Bose
  • 161
  • 1
  • 9
1

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)

etayluz
  • 15,920
  • 23
  • 106
  • 151