如何使用MPMoviePlayerController播放视频?

7
我正在使用以下代码使用MPMoviePlayerController播放视频,但视频没有播放。 有人可以告诉我为什么吗?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];

    [[moviePlayer view] setFrame:[[self view] bounds]];
    [[self view] addSubview: [moviePlayer view]];


    moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

    [moviePlayer play];

你是否指定了文件的扩展名? - Fran Sevillano
7个回答

20

有点奇怪,但是如果你将MPMoviePlayerController作为属性而不是局部变量,它似乎可以正常工作。似乎后台发生了一些事情。我认为这可能与ARC有关。你正在使用ARC吗?

你还过度添加了路径,这也是一个问题:

// You've already got the full path to the documents directory here.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];
// Now you're appending the full path to the documents directory to your bundle path
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
当我在模拟器中运行您的代码时,路径看起来像这样:
/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
它应该只是这个:
/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
所以只需删除此行:
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

然后将您的播放器实例化更改为:

_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];

[[_moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [_moviePlayer view]];

_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

[_moviePlayer play];

因此,您应将MPMoviePlayerController添加为包含视图控制器的属性。


看起来你已经给出了一个清晰的描述。我很久以前就解决了这个问题。问题在于设置框架并将preparetoPlay和fullscreen设为YES。 - Perseus
你好 @Ancientar。你可以在上面的评论中看到我提到了,我忘记为 MPMoviePlayerController 设置 frame,并加入了一个 prepareToPlay 属性。这样就可以正常工作了。 - Perseus
在我的情况下,这是ARC问题。我已经创建了一个mediaplayer的属性,并且它对我有用。感谢您的答案。 - Matt

3

好的,应用程序包和文档目录之间有很大的区别。我建议您仔细了解一下。

首先,视频存储在哪里?

如果您的视频存储在文档目录中,请不要将文档目录路径附加到包路径上。

只需使用filePath变量进行尝试:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL filePath]];

然而,如果文件在应用程序包中(你在XCode中将其添加到项目中),你应该使用jinx的回应中提到的内容。


我也尝试了使用FilePath,屏幕变黑了但视频没有播放。 - Perseus
请注意,我不打算将这些文件添加到我的项目中,我只想从文档目录中获取它们。 - Perseus
任何一个非常简单的例子,展示如何使用MPMoviePlayerController播放视频,都足够了。如果您能帮我解决这个问题,我将不胜感激。 - Perseus
是的,问题可能是视频根本不存在。你是从哪里下载的?它是如何到达文档目录的? - Fran Sevillano
我已经复制并粘贴了它。 对于Webview情况,它可以正常工作。 [webview loadRequest:urlRequest]; 它在webview中加载文件,我可以看到它正在播放。 - Perseus

1
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  
        [moviePlayer.view setFrame:CGRectMake(//set rect frame)];
        moviePlayer.controlStyle =  MPMovieControlStyleDefault; 
        moviePlayer.shouldAutoplay=YES;
        moviePlayer.repeatMode = NO;  
        [moviePlayer setFullscreen:YES animated:NO]; 
        [moviePlayer prepareToPlay];
[self.view addsubview:movieplayer.view];

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];


- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {

    if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
//add your code


    }
}

0

Player是使用我们想要播放的视频的URL进行初始化的(它可以是设备上本地文件的路径或直播URL)。之后,将Player添加为当前视图的子视图。

MPMoviePlayerController类支持以下视频格式

  1. .mov .mpv .3gp .mp4

我不确定这篇文章能对你有多大帮助。我是新手。我按照this article中提供的逐步说明进行操作。


0

尝试直接询问您的捆绑包,而不是手动设置文件路径

NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mov"];

-1
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

希望能帮到你


-1

我花了几分钟来调试问题,但答案非常简单。这是解决方法:

  • 如果你想让MPMoviePlayerViewController从网址播放,请使用以下代码: NSURL *url = [NSURL URLWithString:@"https://www.test.com/anyMovie.mp4"];

  • 如果你想让MPMoviePlayerViewController从应用程序包中播放,请使用以下代码: NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"anyMovie" ofType:@"m4v"]; NSURL *url = [NSURL fileURLWithPath:moviePath];

其余部分相同,只需将属性“movieSourceType”设置如下:

MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentViewController:moviePlayerView animated:YES completion:^{}];

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