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

Tim*_*aev 3 iphone objective-c mpmovieplayercontroller ios4 ios

我在Documents文件夹中有一个名为"Video"的子文件夹.还有somevideo.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];
Run Code Online (Sandbox Code Playgroud)

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

Joh*_*ter 6

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

[videoPlayerView setContentURL: fURL];
Run Code Online (Sandbox Code Playgroud)

这是我从文档文件夹中播放视频的代码片段.我建议的一件事是避免一些自动释放对象.当你有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];
Run Code Online (Sandbox Code Playgroud)

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