MPMoviePlayerViewController播放来自Documents目录的电影 – objective-c

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

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

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

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

 [videoPlayerView setContentURL: fURL]; 

这是我从文档文件夹中播放video的代码片段。 我建议的一件事是避免一些自动释放对象。 当你有viewcontrollers时,特别是在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的子类(我通常对所有视图控制器都这样做)所以对’self’的引用将是对MPMoviePlayerController对象实例的引用

奇怪。 你提出的代码似乎是正确的。 确保您尝试检索的音频的路径未被破坏。