2011-07-23 16 views
7

Quiero reproducir un video por la URL. Veo algunos ejemplos, los códigos como a continuación:cómo iOS reproduce el video por URL

NSString *movieFile= [[NSBundle mainBundle] pathForResource:@"android" ofType:@"mp4"]; 
videoURL=[[NSURL alloc] initFileURLWithPath:movieFile]; 
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:videoURL]; 

Se juega sólo el resource.I local de escribir algo de código como:

NSString* strurl [email protected]"https://s3.amazonaws.com/adplayer/colgate.mp4"; 
videoURL=[NSURL fileURLWithPath:strurl]; 
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:videoURL]; 

pero no hay nada ... why..and cómo reproducir video por la url?

Respuesta

12

Trate

NSURL *url = [NSURL URLWithString: strurl]; 
+0

gracias ~~ Es OK.thanks para su cálida ayuda :) –

+1

no se resuelve el problema? – pasine

17

En siguiente código, estoy jugando un video en internet de un archivo de película se encuentra en un servidor web. No olvide agregar el marco de MediaPlayer e incluir "MediaPlayer/MediaPlayer.h" en el archivo ViewController.h.

En un clic de botón utilización siguiente código:

-(IBAction) playVideo:(id)sender 
    { 

      NSURL *url=[[NSURL alloc] initWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"]; 

      MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url]; 

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 

      moviePlayer.controlStyle=MPMovieControlStyleDefault; 
      moviePlayer.shouldAutoplay=YES; 
      [self.view addSubview:moviePlayer.view]; 
      [moviePlayer setFullscreen:YES animated:YES]; 
    } 

método de notificación:

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

       MPMoviePlayerController *player = [notification object]; 

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

       if ([player respondsToSelector:@selector(setFullscreen:animated:)]) 
       { 
         [player.view removeFromSuperview]; 
       } 
     } 
+0

Gracias por esto :) – swiftBoy

+0

Necesita agregar MPMoviePlayerController * moviePlayer globalmente, de lo contrario no funcionará. – Alok

+0

MPMoviePlayerController ahora está en desuso ... ¿qué hacer para reemplazar? – Simmy

14

Utilice el siguiente código:

- (IBAction)playBtnPressed { 
    NSURL *url = [[NSURL alloc] initWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"]; 
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDonePressed:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer]; 

    moviePlayer.controlStyle=MPMovieControlStyleDefault; 
    //moviePlayer.shouldAutoplay=NO; 
    [moviePlayer play]; 
    [self.view addSubview:moviePlayer.view]; 
    [moviePlayer setFullscreen:YES animated:YES]; 
} 

- (void)moviePlayBackDonePressed:(NSNotification *)notification { 
    [moviePlayer stop]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];        

    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) { 
     [moviePlayer.view removeFromSuperview];  
    } 

    [moviePlayer release]; 
    moviePlayer = nil; 
} 

- (void)moviePlayBackDidFinish:(NSNotification *)notification { 
    [moviePlayer stop]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];            

    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) { 
     [moviePlayer.view removeFromSuperview]; 
    } 
} 
  • Usar la siguiente línea en .h archivar y agregar MediaPlayer Framework en yo ur proyecto

    importación

+2

Esto funcionó para mí, pero necesita agregar esto a su archivo .h '@property (atomic, strong) MPMoviePlayerController * moviePlayer;' y luego sintetizar la propiedad en su archivo .m '@synthesize moviePlayer;' – justinhartman

Cuestiones relacionadas