MPMoviePlayerViewController播放来自Documents目录的电影 - objective-c [英] MPMoviePlayerViewController play movie from Documents directory - objective-c

查看:113
本文介绍了MPMoviePlayerViewController播放来自Documents目录的电影 - objective-c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Documents文件夹中有一个名为Video的子文件夹。并且有一些video.mp4文件。我有这个文件的完整路径,但MPMoviePlayerViewController不想播放本地的视频。这是代码:

I have subfolder called "Video" in Documents folder. And there is somevideo.mp4 file. I have full path to this file but MPMoviePlayerViewController doesn't want to play video from local. Here is code:

NSString *path = [[self getVideoFolderPath] stringByAppendingPathComponent:@"somevideo.mp4"];
NSURL *fURL = [NSURL fileURLWithPath:path];
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:fURL];
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];

它不播放。出现白色屏幕,没有别的。这个代码适用于远程视频,当我把URL放到带有视频文件的网站,但本地不播放。如何获得正确的视频文件文件路径?

It doesn't play. Appears white screen and nothing else. This code works with remote video, when i put URL to website with video file, but local doesn't play. How to get correct file path to video file?

推荐答案

您使用的是4.3之前的iOS吗?如果是这样,你需要这样做

Are you using iOS prior to 4.3? If so, you need to do this

[videoPlayerView setContentURL: fURL];

以下是我从文档文件夹中播放视频的代码片段。我建议的一件事是避免一些自动释放对象。当你有viewcontrollers时,特别是在NSURL上。我知道它应该是安全的,但我从未发现它是100%

Here is a snippet of code from where I play video from the documents folder. One of the things I would recommend is to avoid some of the autorelease objects. Especially on NSURL when you have viewcontrollers. I know it should be safe but I've never found it to be 100%

if ( [[NSFileManager defaultManager] fileExistsAtPath:fullpathVideoFile] == NO )
    {
    NSLog(@"No video file found");
    return self;
    }

playbackURL = [[NSURL alloc] initFileURLWithPath:fullpathVideoFile isDirectory:NO];

if ( playbackURL == nil )
    {
    NSLog(@"playbackURL == nil");
    return self;
    }

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadedMovie:) name:@"MPMoviePlayerLoadStateDidChangeNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerStartedMovie:) name:@"MPMoviePlayerNowPlayingMovieDidChangeNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerFinishedMovie:) name:@"MPMoviePlayerPlaybackDidFinishNotification" object:nil];

[self setContentURL:playbackURL];

在我的情况下,我创建了MPMoviePlayerController的子类(我通常对所有视图控制器都这样做)所以对'self'的引用将是MPMoviePlayerController对象实例

In my case, I created a subclass of MPMoviePlayerController (as I generally do with all view controllers) so the references to 'self' would be to the MPMoviePlayerController object instance

这篇关于MPMoviePlayerViewController播放来自Documents目录的电影 - objective-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆