我在 Documents 文件夹中有一个名为“Video”的子文件夹。还有一些video.mp4 文件。我有这个文件的完整路径,但 MPMoviePlayerViewController 不想从本地播放视频。这是代码:
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 放入带有视频文件的网站时,但本地不播放。如何获取视频文件的正确文件路径?
最佳答案
您使用的是 4.3 之前的 iOS 吗?如果是这样,你需要这样做
[videoPlayerView setContentURL: fURL];
这是我从文档文件夹中播放视频的代码片段。我建议的一件事是避免使用一些自动释放对象。当你有 View Controller 时,尤其是在 NSURL 上。我知道它应该是安全的,但我从未发现它是 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 的子类(就像我通常对所有 View Controller 所做的那样)所以对“self”的引用将指向 MPMoviePlayerController 对象实例
关于iphone - MPMoviePlayerViewController 从 Documents 目录播放电影 - objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7520437/