iOS使用MPMoviePlayerController播放video

我得到这段代码:

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]; 

但我真的不知道如何将video添加到我的项目。 在哪个文件夹我必须把video文件!? 或者我还需要做些什么来添加到我的项目?

编辑:

它在xcode中看起来像这样,是正确的吗? 因为我现在确实收到播放错误。 以前我用一个url来播放这个video,这工作得很好,但与本地文件不是:(

在这里输入图像说明

确定你的包path看起来挺直,下面应该工作。

 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]; 

添加MediaPlayer框架

将其导入到您的文件

 #import <MediaPlayer/MediaPlayer.h> 

创build一个MPMoviePlayerController对象

 MPMoviePlayerController * moviePlayer; 

把这个代码写在你想播放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]; 

按照我上面所承诺的使用HTML5:

  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]; 

这将播放640×480,但如果你熟悉HTML5video标签,你可以自定义相当。

由于您使用的是MPMoviePlayerController而不是UIWebView,因此您可以将您的mp4或文件放在您的资源中,并且XCode / iOS会find它。 确保该文件所在的目录/组是黄色而不是蓝色。 你不希望它是一个相对path。

只需将资源拖到您的项目中。 复制项目到目的地被选中,文件夹的第一个选项被选中,最重要的是,添加到目标!

好的尝试下面的代码:

 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];