2011-11-28 21 views
51

Quiero escribir un pequeño cliente de video iOS y tengo que usar HTTP Live Streaming. Los videos provienen de un Wowza Media Server que admite HTTP Live Streaming, por lo que la implementación del servidor no es mi problema. Ya he visto los videos de WWDC y he leído la documentación de Apple sobre HTTP Live Streaming.Implementación de HTTP Live Streaming en iOS

Pero no hay ningún lugar explicado cómo reproducir los videos en un dispositivo iOS. En una charla-WWDC se mencionó que hay 3 possebilties para mostrar los videos:

  • UIWebView
  • MPMoviePlayerController
  • AVPlayerItem

¿Cuál es la mejor?

¿Y cómo puedo leer las URL de video de una página HTML como estas, que son provistas por el servidor?

<html> 
<head> 
    <title>HTTP Live Streaming Example</title> 
</head> 
<body> 
    <video 
     src="http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8" 
     height="300" width="400" 
    > 
    </video> 
</body> 
</html> 

(Fuente: Apple HTTP Live Streaming Overview)

Realmente no sé por dónde empezar con la codificación ... Tal vez alguien sabe mejor código de ejemplo que el molesto "cosido Stream Player" o puede escribir un pequeño tutorial ...

Respuesta

54

Una implementación corta y al punto. La URL incluida indica una transmisión válida (a partir del 15/12/2015), pero puede reemplazarla con su propia URL en un archivo .m3u8.

Objective-C:

#import <MediaPlayer/MediaPlayer.h> 
@interface ViewController() 
@property (strong, nonatomic) MPMoviePlayerController *streamPlayer; 
@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSURL *streamURL = [NSURL URLWithString:@"http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"]; 

    _streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL]; 

    // depending on your implementation your view may not have it's bounds set here 
    // in that case consider calling the following 4 msgs later 
    [self.streamPlayer.view setFrame: self.view.bounds]; 

    self.streamPlayer.controlStyle = MPMovieControlStyleEmbedded; 

    [self.view addSubview: self.streamPlayer.view]; 

    [self.streamPlayer play]; 
} 

- (void)dealloc 
{ 
    // if non-ARC 
    // [_streamPlayer release]; 
    // [super dealloc]; 
} 

@end 

Swift:

import UIKit 
import MediaPlayer 

class ViewController: UIViewController { 

    var streamPlayer : MPMoviePlayerController = MPMoviePlayerController(contentURL: NSURL(string:"http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8")) 

    //Let's play 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     streamPlayer.view.frame = self.view.bounds 
     self.view.addSubview(streamPlayer.view) 

     streamPlayer.fullscreen = true 
     // Play the movie! 
     streamPlayer.play() 
    } 

} 

respuesta actualizada para los dos idiomas. También MPMoviePlayerController está en desuso en iOS 9, pero puede usar AVPlayerViewController en su lugar. Happy Coding !!!

+0

H Chris. Gracias por el simple ejemplo. Funciona muy bien. Me pregunto ¿cómo se crea un archivo .m3u8 a partir de un video? – Isuru

+3

Mi sitio ha estado inactivo por un tiempo, pero aún puede acceder a una publicación que escribí aquí: http://www.christopherlavender.com/tag/http-live-streaming/. Además, hay algunos tutoriales más antiguos que todavía están bien. La clave es que si va a "poner sus propios videos" HLS, necesitará las herramientas de segmentación de líneas de comando del sitio web de desarrolladores de Apple. Funcionan bien, pero la documentación es irregular en el mejor de los casos. – GnarlyDog

+0

Gracias por el tutorial. Lo investigaré de inmediato. :) – Isuru

5

Si apuntas un UIWebView a esa URL objetivo m3u8, simplemente funcionará.

+7

lol esa es mi frase favorita. Me encanta cuando las cosas "simplemente funcionan" – uofc

Cuestiones relacionadas