iOS使用MPMoviePlayerController播放视频

7

I got this piece of code:

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中是这样的,这正确吗? 因为我现在遇到了播放错误。 以前我使用url来播放这个视频,这很好用,但是本地文件却不行 :(

enter image description here

4个回答

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

可以了!非常感谢。我对iOS开发还比较新手。我的方法为什么不能达到预期的效果呢? - krackmoe
如果您想使用UIWebView,可以使用HTML5播放视频,如果您更熟悉它的话。我将在下面附上代码。 - apollosoftware.org
如果你想玩弄它,請將翻譯後的文本作為答案附上。 - apollosoftware.org

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

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视频标签,您可以自定义得更加深入。

毫无疑问,我大多数时候使用HTML5进行播放。它的某些特性让人感觉更加清晰明了。此外,与iOS的多媒体框架相比,它还允许更多的自定义功能,这得益于WebKit技术。 - apollosoftware.org

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

1
应该也要使用 NSBundle 获取路径。 - Wain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接