从文档文件夹中读取视频文件

时间:2014-12-01 10:44:37

标签: ios iphone xcode ipad mpmovieplayercontroller

我的代码运行正常并在我的应用中向我显示视频:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;

我需要的是从文档文件夹中读取相同的文件,它存在,所以我做的是:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *str = [NSString stringWithFormat:@"%@/MyCoolApp/Animations/%@", documentsPath,@"movie"];
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:str ofType:@"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;

文件在那里,但没有播放,只是加载。

1 个答案:

答案 0 :(得分:1)

您正在获取文档文件夹的路径,然后尝试在主包中找到它。

尝试以下方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"/MyCoolApp/Animations/movie.mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:path];

MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;