2012-03-05 45 views
7

Estoy creando un objeto MPMoviePlayerController y estoy transmitiendo un video en modo de pantalla completa.MPMoviePlayerController no elimina la vista al hacer clic en

Estoy usando un UIViewController para mostrar la vista de la película.

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    //http://www.youtube.com/watch?feature=player_detailpage&v=ebeQaznNcmE 
    NSURL *url = [NSURL URLWithString:@"http://a1408.g.akamai.net/5/1408/1388/2005110405/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_mpeg4.mp4"]; 
    MPMoviePlayerController *mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url]; 
    mPlayer.view.frame = gMainView.frame; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePlayBackDidFinish:) 
              name:MPMoviePlayerPlaybackDidFinishNotification 
              object:mPlayer]; 
    mPlayer.shouldAutoplay = YES; 
    mPlayer.controlStyle = MPMovieControlStyleFullscreen; 
    [gMainView addSubview:mPlayer.view]; 
    [mPlayer prepareToPlay]; 
    [mPlayer setFullscreen:YES animated:YES]; 
    [mPlayer play]; 
} 


- (void)moviePlayBackDidFinish:(NSNotification*)notification { 
    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue]; 
    if (reason == MPMovieFinishReasonPlaybackEnded) { 
     //movie finished playing 
    } 
    else if (reason == MPMovieFinishReasonUserExited) { 
     //user hit the done button 
     MPMoviePlayerController *moviePlayer = [notification object]; 

     [[NSNotificationCenter defaultCenter] removeObserver:self  
                 name:MPMoviePlayerPlaybackDidFinishNotification 
                 object:moviePlayer]; 

     if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) { 
      [moviePlayer.view removeFromSuperview]; 
     } 
     [moviePlayer release]; 
    } 
    else if (reason == MPMovieFinishReasonPlaybackError) { 
     //error 
    } 
} 

al hacer clic hecho, el video visual se elimina de la pantalla, pero los controles no se eliminan de la pantalla y la vista no se elimina de la pantalla.

El control llega a "// usuario pulse el botón hecho". Ejecuta el código para eliminar la vista de la supervista, comprobé agregando registros, pero los controles no se eliminan de la pantalla y la vista no se elimina de la pantalla. ¿Qué estoy haciendo mal?

EDIT:

Si uso MPMoviePlayerViewController entonces ni siquiera esperó a que pulse Hecho. Una vez que el video se completa, elimina automáticamente la vista. Pero no quiero eso.

EDIT:

Si quito "[mPlayer setFullscreen: SÍ animada: SÍ]", entonces al hacer clic en Hecho, la vista se elimina completamente. Pero el video no se muestra en pantalla completa y la barra de estado se vuelve gris, que es nuevamente lo que no quiero.

+0

Usted tomar muchas medidas para describir lo que no quiere, pero, al menos para mí, esto todavía no es realmente afirmar lo que realmente quiere lograr. – Till

+0

Los controles no se eliminan de la pantalla y la vista del reproductor no se elimina de la pantalla. – Anand

+1

Pruebe esta solución: http://stackoverflow.com/questions/6142571/mpmovieplayer-done-button-issue/6142685#6142685 – Till

Respuesta

10

El siguiente código funcionó para mí, espero que te ayude también.

-(IBAction)playVedio:(id)sender{ 
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 

    [[mp moviePlayer] prepareToPlay]; 
    [[mp moviePlayer] setUseApplicationAudioSession:NO]; 
    [[mp moviePlayer] setShouldAutoplay:YES]; 
    [[mp moviePlayer] setControlStyle:2]; 
    [[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 
    [self presentMoviePlayerViewControllerAnimated:mp]; 

} 

-(void)videoPlayBackDidFinish:(NSNotification*)notification { 

    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 

    [mp.moviePlayer stop]; 
    mp = nil; 
    [mp release]; 
    [self dismissMoviePlayerViewControllerAnimated]; 
} 
+0

Por favor, Sirji también agrega algo más con el código que otros puedan entender fácilmente tu respuesta: ¿qué has hecho y cuál era el problema con el código? Pregunta qué has hecho para superarlo, de todos modos gracias por la solución:) – MKJParekh

+0

vl cuídate la próxima vez ... !! :) – Sirji

+4

No deberías simular votos tan descaradamente. – Till

0
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    id presentedViewController = [window.rootViewController presentedViewController]; 
    NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil; 

    if (window && [className isEqualToString:@"AVFullScreenViewController"]) { 

     return UIInterfaceOrientationMaskAll; 

    } else { 

     UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; 

     if(UIInterfaceOrientationIsPortrait(interfaceOrientation)) 

     { 

     } 

     else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) 

     { 


     } 

     return UIInterfaceOrientationMaskPortrait; 

     CGRect frame = [UIScreen mainScreen].applicationFrame; 
     CGSize size = frame.size; 
     NSLog(@"%@", [NSString stringWithFormat:@"Rotation: %s [w=%f, h=%f]", 
         UIInterfaceOrientationIsPortrait(interfaceOrientation) ? "Portrait" : "Landscape", 
         size.width, size.height]); 
    } 
} 
Cuestiones relacionadas