使用Cocoa-Touch从文档目录播放下载的视频

时间:2012-07-18 19:47:46

标签: ios cocoa-touch mpmovieplayercontroller nsdocumentdirectory

我正在尝试让我的应用播放已下载到文档目录的视频文件。我知道文件正在下载,但我似乎无法播放文件,这是我的代码:

-(IBAction)play{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/piggy.m4v"];

NSURL *movieURL = [NSURL fileURLWithPath:path];


_player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[self.view addSubview:_player.view];

_player.controlStyle = MPMovieControlStyleDefault;
_player.shouldAutoplay = YES;


[_player setFullscreen:YES animated:YES];


[_player play];

}

2 个答案:

答案 0 :(得分:11)

这看起来像某种bug,但你必须设置你的路径:

 NSString *vidPath = [[NSBundle mainBundle] pathForResource:@"promo" ofType:@"mp4"];
 NSURL *url = [NSURL fileURLWithPath:vidPath isDirectory:NO]; //THIS IS THE KEY TO GET THIS RUN :) 
 [introPlayer setContentURL:url];

答案 1 :(得分:0)

问题将在这一行:NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/piggy.m4v"];

将其更改为NSString *path = [documentsDirectory stringByAppendingPathComponent:@"piggy.m4v"];