iOS使用MPMoviePlayerController播放视频

时间:2013-07-10 19:07:34

标签: iphone ios ios5 ios6 ios6.1

我得到了这段代码:

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

但我真的不知道如何将视频添加到我的项目中。在哪个文件夹中我必须放置视频文件!?或者我是否必须做其他事情才能将其添加到我的项目中?

编辑:

在xcode中看起来像这样,是否正确? 因为我现在确实收到播放错误。 以前我使用网址来播放这个视频,这个效果很好,但是这个文件在本地没有:(

enter image description here

4 个答案:

答案 0 :(得分:13)

好的,您的捆绑路径看起来很突然,下面应该可以正常工作。

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 :(得分:8)

添加MediaPlayer框架

将其导入您的文件

#import <MediaPlayer/MediaPlayer.h>

创建MPMoviePlayerController的对象

MPMoviePlayerController * moviePlayer;

将此代码写入您想要播放视频的位置

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

答案 2 :(得分:1)

按照我上面的承诺使用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];

这将以640x480播放,但如果您熟悉HTML5视频标签,则可以进行大量自定义。

答案 3 :(得分:0)

由于您使用的是MPMoviePlayerController而不是UIWebView,因此您可以将mp4或文件放在资源中,XCode / iOS会找到它。确保该文件所在的目录/组是黄色而不是蓝色。你不希望它成为相对路径。

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

好的,请尝试以下代码:

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