7

我得到了这段代码:

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 播放这个视频,效果很好,但是在本地没有这个文件:(

在此处输入图像描述

4

4 回答 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];
于 2013-07-10T19:33:54.143 回答
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];
于 2014-04-01T12:28:32.783 回答
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 视频标签,则可以进行大量自定义。

于 2013-07-10T19:41:15.797 回答
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];
于 2013-07-10T19:11:49.077 回答