5

Estoy tratando de reproducir un archivo de video desde la carpeta de recursos en iPhone con iOS 4.1. El MPMoviePlayerViewController muestra la vista, pero solo dice "Cargando película ..." para siempre con el indicador de actividad girando. Alguien sabe la causa de esto? Lo he intentado con varios formatos de video que se supone que funcionan en iPhone, el mismo resultado cada vez.MPMoviePlayerController "Cargando película ..."

El reproductor de películas responde a acciones como mostrar u ocultar los controles.

Mi código:

-(void) playVideoNamed:(NSString *)videoName 
{ 
    // I get the path like this so the user can enter extension in the filename 
    NSString *path = [NSString stringWithFormat:@"%@/%@", 
         [[NSBundle mainBundle] resourcePath], videoName]; 
    NSLog(@"Path of video file: %@", path); 

    RootViewController *rootController = [(DerpAppDelegate *)[[UIApplication sharedApplication] delegate] viewController]; 

    NSURL *url = [NSURL URLWithString:path]; 

    MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 
    vc.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 

    [rootController presentMoviePlayerViewControllerAnimated:vc]; 
    [vc.moviePlayer prepareToPlay]; // Not sure about this... I've copied this code from various others that claims this works 
    [vc.moviePlayer play]; 
} 

Respuesta

20

enteraron de la cuestión:

NSURL *url = [NSURL URLWithString:path]; 

deben sustituirse por:

NSURL *url = [NSURL fileURLWithPath:path]; 

difíciles de detectar que uno ...

+0

Awesome! ¡Salvaste mi día! ¡Gracias una tonelada! – Ayyappa

+0

@Accatyyc, ¿puedo escribir como 'NSURL * url = [[NSURL alloc] initFileURLWithPath: ruta];' – Hemang

+0

puede escribir 'url = [NSURL fileURLWithPath: ruta esDirectorio: NO];' – Accatyyc

0

Estaba realizando las líneas de código en una hilo diferente, primer envío al hilo principal lo arregló:

dispatch_async(dispatch_get_main_queue(), ^(void){ 
     MPMoviePlayerViewController* theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL: videoURL]; 
     [self presentMoviePlayerViewControllerAnimated:theMovie]; 

}); 
Cuestiones relacionadas