2012-01-30 5 views
28

Quiero reproducir videos de YouTube desde mi aplicación de iPhone. Tengo que probado vídeos de YouTube juego en mi aplicación para el iPhone con los siguientes códigos,¿Reproduce videos de YouTube en la aplicación para iPhone sin usar UIWebView?

[self playVideo:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q" frame:CGRectMake(20, 70, 280, 250)]; 

- (void)playVideo:(NSString *)urlString frame:(CGRect)frame 
{ 
    NSString *embedHTML = @"\ 
    <html><head>\ 
    <style type=\"text/css\">\ 
    body {\ 
    background-color: transparent;\ 
    color: white;\ 
    }\ 
    </style>\ 
    </head><body style=\"margin:0\">\ 
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \ 
    width=\"%0.0f\" height=\"%0.0f\"></embed>\ 
    </body></html>"; 
    NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height]; 
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame]; 
    [videoView loadHTMLString:html baseURL:nil]; 
    [self.view addSubview:videoView]; 
    [videoView release]; 
    NSLog(@"%@",html); 
} 

embargo, este código no está funcionando para mí. Y también he tratado con MPMoviePlayerController el código es,

NSURL *fileURL = [NSURL URLWithString:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q"]; 
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)]; 
[self.view addSubview:moviePlayerController.view]; 
moviePlayerController.fullscreen = YES; 
[moviePlayerController play]; 

Esto también no está funcionando para mí. ¿Puede alguien decirme dónde hice mal y sugerirme alguna idea? Quiero reproducir los videos de YouTube en MPMoviewPlayerController en lugar de usar UIWebView. Gracias por adelantado.

+0

Relacionado: http://stackoverflow.com/questions/1779511/play-youtube-videos-with-mpmovieplayercontroller-instead-of-uiwebview –

Respuesta

6

Mr.Yuvaraj.M su código anterior es correcto. Ejecute el proyecto en su dispositivo iPhone/iPod touch. Es posible que los videos de youtube no sean compatibles/no funcionen en Simulator. Por favor revisa esto. Gracias.

+0

Mr.Gopinath gracias por su respuesta. Verificare tu respuesta. Gracias. –

1

En lugar del código HTML insertado que está utilizando, vaya al sitio web de YouTube, busque un video, use el botón Compartir y luego el botón Insertar para obtener un ejemplo de HTML incrustado. Use ese patrón para incrustar el video en lugar de la versión de Flash que está usando actualmente.

8

Simplemente use XCDYouTubeVideoPlayerViewController. Sin problemas agrega video de youtube a su aplicación sin la necesidad de UIWebView.

Utiliza la descarga progresiva + MoviePlayer y AVFoundation frameworks. Solo bríndele una identificación de video de youtube y estará listo.

Es compatible con pantalla completa & no a pantalla completa y FUNCIONA CON EL SIMULADOR DE iOS !!!

Ejemplo:

XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"]; 
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController]; 
+0

agrego esta biblioteca usando pod, y qué encabezado quiero usar para esto. –

+0

@ R.Mohan debe ser '#import ' – Hlung

Cuestiones relacionadas