2013-07-10 22 views
7

ho ottenuto questo pezzo di codice:iOS riproduzione video con MPMoviePlayerController

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:@"/Resources/disc.mp4"]]; 
    theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen; 
    theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2)); 
    UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow]; 
    [theMoviPlayer.view setFrame:backgroundWindow.frame]; 
    [backgroundWindow addSubview:theMoviPlayer.view]; 
    [theMoviPlayer play]; 

Ma io davvero non so come aggiungere il video per il mio progetto. In quale cartella devo mettere il file video !? O devo fare qualcos'altro per aggiungerlo al mio progetto?

EDIT:

Ecco come si presenta in Xcode, è corretto? Perché ottengo un errore di riproduzione adesso. In precedenza ho usato un URL per riprodurre il video e questo ha funzionato abbastanza bene, ma con questo file localmente non :(

enter image description here

risposta

13

Ok vostro percorso fascio sembra sollevato, sotto dovrebbe funzionare.

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

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen; 
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2)); 
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow]; 
[theMoviPlayer.view setFrame:backgroundWindow.frame]; 
[backgroundWindow addSubview:theMoviPlayer.view]; 
[theMoviPlayer play]; 
+0

Works! Grazie tante. Sono abbastanza nuovo per lo sviluppo di iOS. Qual è la ragione per cui il mio approccio non ha funzionato come previsto? – krackmoe

+0

Se si desidera utilizzare UIWebView è possibile utilizzare HTML5 per riprodurre video se si è più a suo agio con esso. Allegherò il codice qui sotto. – ApolloSoftware

+0

allegato come una risposta se si desidera giocare con esso. – ApolloSoftware

0

Dal momento che si sta utilizzando MPMoviePlayerController e non UIWebView è possibile inserire il vostro MP4 o file nel Risorse e XCode/iOS lo troveranno.Assicurarsi che la directory/gruppo in cui si trova il file sia giallo non blu.Non vuoi che si tratti di un percorso relativo

Basta trascinare la risorsa nel tuo progetto. Copia gli oggetti in destinazione è selezionata, la prima opzione di Cartelle è selezionata e, soprattutto, aggiungi al target!

Ok provare il codice qui sotto:

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

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen; 
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2)); 
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow]; 
[theMoviPlayer.view setFrame:backgroundWindow.frame]; 
[backgroundWindow addSubview:theMoviPlayer.view]; 
[theMoviPlayer play]; 
+1

Dovrebbe anche essere ottenere il percorso utilizzando 'NSBundle' – Wain

+0

concordato, Wain .... – ApolloSoftware

+0

Ho modificato il mio post principale, per favore dare un'occhiata di nuovo – krackmoe

1

utilizzando HTML5 come promesso in precedenza:

NSString *videoTitle = @"disc.mp4"; 
    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; 
    NSString *playPath = [NSString stringWithFormat:@"<center><video width=\"640\" height=\"480\" controls><source src=\"%@\" media=\"all and (max-width:1024px)\"></video></center>",videoTitle]; 


    [webView loadHTMLString:playPath baseURL:baseURL]; 

Questo giocherà a 640x480, ma se si ha familiarità con HTML5 tag video, è possibile personalizzare piuttosto pesantemente.

+0

Grazie, buono a sapersi, anche se non ne ho bisogno in questo momento! – krackmoe

+0

Senza dubbio. Io uso HTML5 per la riproduzione la maggior parte del tempo. Qualcosa su di esso lo fa sentire più pulito. Inoltre con WebKit consente una maggiore personalizzazione rispetto ai Multimedia Framework di iOS. – ApolloSoftware

8

Aggiungere MediaPlayer quadro

importazione al file

#import <MediaPlayer/MediaPlayer.h> 

Creare un oggetto di MPMoviePlayerController

MPMoviePlayerController * moviePlayer; 

scrivere questo codice in cui si vuole riprodurre video

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"spacetest.mp4" ofType:nil]; 
NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
[self.view addSubview:moviePlayer.view]; 
moviePlayer.fullscreen = YES; 
[moviePlayer play]; 
Problemi correlati