2010-10-18 15 views
20

Supongamos iOS 3.2 o posterior. ¿Cuál es la secuencia de comando adecuada para jugar un movimiento con los controles inicialmente ocultos? Cuando la película se está reproduciendo, el usuario tiene la opción de etiquetar en la pantalla y mostrar los controles.¿Cómo ocultar el control antes de reproducir la película MPMoviePlayerController?

Gracias!

Mi (control no oculta) código:

- (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];  
    } 
} 

Respuesta

35

[Actualización] Según lo propuesto por @ReinYem, una solución mucho mejor es confiar en un MPMoviePlayerLoadStateDidChangeNotification en lugar de un temporizador.

En realidad, la siguiente solución no debe considerarse más:

Establecer controlStyle propiedad a MPMovieControlStyleNone inicialmente, y luego otra vez en MPMovieControlStyleFullscreen un segundo más tarde usando un [performSelector:withObject:afterDelay:1]. Funciona bien, los controles no aparecen hasta que el usuario toca el video.

+1

Buen consejo. También descubrí que si establezco el estilo de control en 'MPMovieControlStyleFullscreen' inmediatamente después de ejecutar Play, se obtiene el mismo resultado. Entonces, ¿tal vez no hay razón para especificar un retraso arbitrario de 1 segundo? – curthipster

+0

También me ha funcionado una demora de un segundo. Intenté medio segundo, pero eso fue demasiado rápido. –

+0

Sí, yo también necesito un retraso aún preguntándome por qué tengo que introducir tal retraso. –

34

Uso llamada de retorno de un temporizador:

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

Con la función de devolución de llamada:

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

} 
+0

Me gusta mucho esta solución, ya que cubre el comportamiento deseado con mucha más precisión que un temporizador, eliminando por completo la condición de carrera de jugar, pero luego pausar u ocultar (suponiendo que limpie este observador también en esos casos). –

+0

No solo es esta la "mejor" solución, es la solución correcta. El otro simplemente falla ante condiciones de carrera impredecibles. – kevlar

+3

No estoy seguro de que esto todavía funcione en iOS 6. Parece ser aleatorio si el estado de carga cambia demasiado pronto. Observar MPMoviePlayerPlaybackStateDidChangeNotification para un cambio en playbackState a MPMoviePlaybackStatePlaying parece más robusto. –

10
player.moviePlayer.controlStyle = MPMovieControlStyleNone; 

¿Es la nueva forma de hacerlo. :)

+0

funciona como un encanto – Hammer

Cuestiones relacionadas