How to hide control before MPMoviePlayerController movie is played?

27,615

Solution 1

[Update] As proposed by @ReinYem, a much better solution is to rely on a MPMoviePlayerLoadStateDidChangeNotification instead of a timer.

Actually, the following solution should not be considered anymore:

Set controlStyle property to MPMovieControlStyleNone initially, and then set it to MPMovieControlStyleFullscreen one second later using a [performSelector:withObject:afterDelay:1]. It works well, controls do not appear until user taps on video.

Solution 2

Use a callback instead of a timer :

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidecontrol) 
                                                 name:MPMoviePlayerLoadStateDidChangeNotification 
                                               object:playerView.moviePlayer];

With call back function :

- (void) hidecontrol {
[[NSNotificationCenter defaultCenter] removeObserver:self     name:MPMoviePlayerNowPlayingMovieDidChangeNotification object:playerView.moviePlayer];
[playerView.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];

}

Solution 3

player.moviePlayer.controlStyle = MPMovieControlStyleNone;

Is the newest way to do it. :)

Share:
27,615
ohho
Author by

ohho

I run and write code.

Updated on July 09, 2022

Comments

  • ohho
    ohho almost 2 years

    Assume iOS 3.2 or later. What is the proper sequence of command to play a move with the controls initially hidden. When the movie is playing, the user has the option to tag on screen and show controls.

    Thanks!

    My (control not hidden) code:

    - (void)playMovie
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Test" ofType:@"m4v"]];  
        MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  
    
        // Register to receive a notification when the movie has finished playing.  
        [[NSNotificationCenter defaultCenter] addObserver:self  
                                                 selector:@selector(movieDone:)  
                                                     name:MPMoviePlayerPlaybackDidFinishNotification  
                                                   object:moviePlayer];  
    
        [[NSNotificationCenter defaultCenter] addObserver:self  
                                                 selector:@selector(movieState:)  
                                                     name:MPMoviePlayerNowPlayingMovieDidChangeNotification  
                                                   object:moviePlayer];  
    
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(movieReady:) 
                                                     name:MPMoviePlayerLoadStateDidChangeNotification 
                                                   object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullScreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:moviePlayer];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullScreen:) name:MPMoviePlayerWillExitFullscreenNotification object:moviePlayer];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishPlayback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    
    
        if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {  
            moviePlayer.controlStyle = MPMovieControlStyleDefault; // MPMovieControlStyleNone MPMovieControlStyleEmbedded MPMovieControlStyleFullscreen MPMovieControlStyleDefault
            moviePlayer.scalingMode = MPMovieScalingModeAspectFill; // MPMovieScalingModeAspectFit MPMovieScalingModeAspectFill
        }   
    }
    
    - (void) movieReady:(NSNotification*)notification 
    {
        MPMoviePlayerController *moviePlayer = [notification object];  
    
        if ([moviePlayer loadState] != MPMovieLoadStateUnknown) {
            // Remove observer
            [[NSNotificationCenter defaultCenter] removeObserver:self  
                                                            name:MPMoviePlayerLoadStateDidChangeNotification  
                                                          object:nil];
    
            // When tapping movie, status bar will appear, it shows up
            // in portrait mode by default. Set orientation to landscape
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    
            // Add movie player as subview
            [[self view] addSubview:[moviePlayer view]];   
    
            // Play the movie
            [moviePlayer play];
            [moviePlayer setFullscreen:YES animated:YES];       
        }
    }
    
  • curthipster
    curthipster about 13 years
    Nice tip. I also found that if I set the control style to MPMovieControlStyleFullscreen immediately after calling play, it achieves the same result. So maybe there's no reason to specify an arbitrary 1 second delay?
  • Tod Cunningham
    Tod Cunningham almost 13 years
    A one second delay worked for me as well. I tried a half second, but that was too quick.
  • Rob Rix
    Rob Rix about 12 years
    I really like this solution, as it covers the desired behaviour far more precisely than a timer, removing the play-but-then-pause-or-hide race condition entirely (assuming you clean up this observer in those cases too).
  • kevlar
    kevlar about 12 years
    Not only is this the "better" solution, it's the correct solution. The other simply fails in the face of unpredictable race conditions.
  • David Snabel-Caunt
    David Snabel-Caunt over 11 years
    Not sure this is still working on iOS 6. It seems to be random whether the loadState changes too early. Observing MPMoviePlayerPlaybackStateDidChangeNotification for a change in playbackState to MPMoviePlaybackStatePlaying seems more robust.
  • VARUN ISAC
    VARUN ISAC over 10 years
    Yes, i too need a delay still wondering why i need to introduce such delay.
  • Ricardo Sanchez-Saez
    Ricardo Sanchez-Saez almost 10 years
    @DavidCaunt: Your solution no longer works. On iOS 7, setting the movie controls there show them immediately.
  • eyuelt
    eyuelt almost 10 years
    Could you explain why you remove the observer for MPMoviePlayerNowPlayingMovieDidChangeNotification?