2012-05-03 15 views
9

iOS 5.1, ecco il codice:MPMoviePlayerController mi dà una vista vuoto nero, nessuna riproduzione video

MPMoviePlayerController *player = 
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; 
    [player prepareToPlay]; 
    [player.view setFrame: self.view.bounds]; // player's frame must match parent's 
    [self.view addSubview: player.view]; 
    // ... 
    [player play]; 

in realtà, questo è proprio lo stesso come il riferimento mela dire, ma quando si fa clic sul pulsante per usa questa funzione, c'è solo un rettangolo nero che rimane lì, nessun vedio che suona, nessun suono accade e nessuna cotta, quindi voglio sapere come fare questo lavoro

+0

il mio codice funziona o no? – Dinesh

risposta

1

Prova questo codice qui sotto per riprodurre video dal tuo progetto Risorsa :

NSBundle *bundle = [NSBundle mainBundle]; 
    NSString *moviePath = [bundle pathForResource:@"map" ofType:@"mp4"]; 
    NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain]; 

    MPMoviePlayerController *player = 
    [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
    [player prepareToPlay]; 
    [player.view setFrame: self.view.bounds]; // player's frame must match parent's 
    [self.view addSubview: player.view]; 
    // ... 
    [player play]; 

Grazie ..!

+0

Terminazione dell'app a causa di eccezione non rilevata 'NSInvalidArgumentException', motivo: '*** - [NSURL initFileURLWithPath:]: parametro stringa nil' e Grazie per il tuo aiuto, ma avevo già provato questo tipo di codice, questa è la console informazioni –

+0

aggiungi il tuo video a 'copia risorse bundle'. –

1

Basandosi sulla risposta di @ Dinesh, la creazione dell'URL non è corretta. Il valore di

[NSURL URLWithString:@"map.mp4"] 

sarà nil poiché @ "map.mp4" non è un URL valido. Per creare un URL valido dal pacchetto dell'app, fai ciò che Dinesh dice. Per creare un URL remoto devi fare qualcosa come

[NSURL URLWithString:@"http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"] 

L'URL deve contenere tutte le sue parti: protocollo, host, percorso, file.

Cheers, Felipe

+0

Grazie, ma in realtà voglio utilizzare un file mp4 locale, non uno stream, [NSBundle mainBundle] sembra funzionare. –

+1

Sono contento che abbia funzionato per te. Per favore considera di assegnare la risposta a Dinesh. È meglio anche per te, dal momento che non assegnarlo danneggerà la tua reputazione. – fsaint

17

Ci scusiamo per molto tardi la risposta. La soluzione è un po 'strana. Ma mi ha aiutato ad andare avanti affrontando lo stesso problema.

MPMovieSourceTypeStreaming è la cosa che hai perso.

MPMoviePlayerController *player = 
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; 
[player prepareToPlay]; 
[player.view setFrame: self.view.bounds]; // player's frame must match parent's 
player.movieSourceType = MPMovieSourceTypeStreaming; 
[self.view addSubview: player.view]; 
// ... 
[player play]; 
+0

Questo non era il problema del poster originale, ma era sicuramente il mio! – shulmey

+0

so @shulmey, è stato d'aiuto? –

+4

Il mio problema era che non ho impostato la proprietà movieSourceType. Quindi se aiuta qualcuno. assicurati di aver impostato quella proprietà. Per impostazione predefinita, deve essere impostato su MPMovieSourceTypeUnknown. Quando l'ho messo in MPMovieSourceTypeFile. Ha funzionato per me. –

0

il lettore dovrebbe essere una proprietà del controller della vista, qualcosa di simile:

.h:

@property(nonatomic,weak)MPMoviePlayerController *moviePlayer; 

.m:

MPMoviePlayerController *player = 
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; 
    [player prepareToPlay]; 
    [player.view setFrame: self.view.bounds]; // player's frame must match parent's 
    //set the player to your property 
    self.moviePlayer=player; 

    [self.view addSubview: self.moviePlayer.view]; 
    // ... 
    [self.moviePlayer play]; 
0

Utilizzare MPMoviePlayerViewController Perché del file MP4. Quando usi MOV allora funziona perfettamente !!

MPMoviePlayerViewController *moviePlayerViewController; 

-(void)PlayVideoController:(NSString*)videoUrl1 { 
    NSURL *fileURL = [NSURL URLWithString:videoUrl1]; 
    moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL]; 

    // Register for the playback finished notification. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer]; 

    //Present 
    [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController]; 

    // Play the movie! 
    moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
    [moviePlayerViewController.moviePlayer play]; 
} 

-(void)myMovieFinishedCallback:(NSNotification*)aNotification { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer]; 
    [moviePlayerViewController release], moviePlayerViewController = nil; 
} 
4

permette di inviare il vostro messaggio

[player play] 

troppo presto.

Usare la seguente osservazione di notifica

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:player]; 

per ascoltare la LoadState per passare alla giocabile. Per verificare l'aggiornamento, effettuare le seguenti operazioni:

- (void)playMovie:(NSNotification *)notification { 
    MPMoviePlayerController *player = notification.object; 
    if (player.loadState & MPMovieLoadStatePlayable) 
    { 
     NSLog(@"Movie is Ready to Play"); 
     [player play]; 
    } 
} 

Il film dovrebbe iniziare a essere riprodotto una volta pronto.

1

Aggiungi il tuo lettore di film View Controller sulle finestre principali come:

MPMoviePlayerController *player = 
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]]; 
    [player prepareToPlay]; 
    [player.view setFrame: self.view.bounds]; 

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
[appDelegate.window addSubview: player.view]; 

[player play]; 

spero che funzionerà!

0

Potrebbe essere necessario verificare l'URL del video per gli spazi vuoti. Il seguente codice funziona per me.

- (IBAction)btnPlayVideo:(id)sender { 

    self.strVideoUrl = [self.strVideoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", self.strVideoUrl]]; 
    NSLog(@"Url to play = %@", self.strVideoUrl); 

     self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 

     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(moviePlaybackComplete:) 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:self.moviePlayerController]; 
     [self.moviePlayerController.view setFrame:self.view.bounds]; 
     [self.view addSubview:self.moviePlayerController.view]; 
     self.moviePlayerController.shouldAutoplay = YES; 
     self.moviePlayerController.fullscreen = YES; 
     self.moviePlayerController.controlStyle=NO; 
     [self.moviePlayerController prepareToPlay]; 
     self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming; 

     [self.moviePlayerController play]; 

} 

- (void)moviePlaybackComplete:(NSNotification *)notification 
{ 
    self.moviePlayerController = [notification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:self.moviePlayerController]; 

    [self.moviePlayerController.view removeFromSuperview]; 
    self.closeVideAdProp.hidden = NO; 
    btnCloseAd.hidden=FALSE; 
} 
Problemi correlati