2011-05-17 6 views
15

¿Cómo puedo reproducir un video .mp4 o .mov desde una URL de Internet o desde un archivo local en iOS?Cómo reproducir un video desde una URL local o de un servidor en iOS

+1

posible duplicado de [Reproducción de un archivo de video desde el servidor en una aplicación de Iphone] (http://stackoverflow.com/questions/5015165/playing-a-video-file-from-server-in-an-iphone-app) –

+0

Duplicado de http://stackoverflow.com/questions/5977330/play-video-by-default-in-full-screen/5977396#5977396 –

Respuesta

28

1.En primer lugar añadir MediaPlayer.framework en XCode

2. A continuación, añadir import < MediaPlayer/MediaPlayer.h> en el archivo .h de viewController

3.Now implementar este código en tu viewDidLoad Método

 //NSString *filepath = [[NSBundle mainBundle] pathForResource:@"aaa" ofType:@"mp4"]; 
    //NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 

    NSURL *fileURL = [NSURL URLWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"]; 

    moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
    [moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)]; 
    [self.view addSubview:moviePlayerController.view]; 
    moviePlayerController.fullscreen = YES; 
    [moviePlayerController play]; 

Por favor Orientación añadir este código

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
      if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
     [moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)]; 
    } else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     [moviePlayerController.view setFrame:CGRectMake(0, 0, 480, 320)]; 
    } 
    return YES; 
} 

En este código moviePlayerController es MPMoviePlayerController declarada en el archivo .h

9

Ésta es una cuestión de edad, pero sigue siendo relevante y el IOS 9 ha desaprobado MPMoviePlayerController. Lo nuevo para usar si AVMoviePlayer, código de ejemplo:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"vid" ofType:@"mp4"]; 
NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
self.avPlayer = [AVPlayer playerWithURL:fileURL]; 

AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer]; 
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 
layer.frame = self.view.bounds; 
[self.view.layer addSublayer: layer]; 

[self.avPlayer play]; 
3

Por favor, intente esta su perfecto estado de funcionamiento para mí,

var moviePlayer:MPMoviePlayerController! 
override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    let url:NSURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")! 
    moviePlayer = MPMoviePlayerController(contentURL: url) 

    moviePlayer.view.frame = CGRect(x: 20, y: 20, width: UIScreen.mainScreen().bounds.size.width-40, height: UIScreen.mainScreen().bounds.size.height/2) 

    self.view.addSubview(moviePlayer.view) 
    moviePlayer.fullscreen = true 

    moviePlayer.controlStyle = MPMovieControlStyle.Embedded 

} 

Y por favor, activar la configuración de aplicación de seguridad Transporte -> Permitir Loads- arbitraria -> SÍ en el archivo Plist.

Cuestiones relacionadas