从UITableView播放video

我的主要目标是能够点击表格视图项目并加载video。 表视图填充文档目录的内容,我已经能够成功地做到这一点,并将文件名添加到单元格的标签,我已经完成了以下代码:

void)viewDidLoad { [super viewDidLoad]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder NSString *dataPath = documentsDirectory; filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:dataPath error:nil]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [filePathsArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"SimpleTableCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; } cell.textLabel.text = [filePathsArray objectAtIndex:indexPath.row]; return cell; } 

然而,我觉得困难的部分是打开文件的一部分。 这是我的代码到目前为止:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"didSelectRowAtIndexPath"); myURL=[NSURL URLWithString:[filePathsArray objectAtIndex:indexPath.row]]; MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:myURL]; [self.view addSubview:moviePlayerController.view]; moviePlayerController.fullscreen = YES; [moviePlayerController play]; [self setController:moviePlayerController]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

当用户点击一个UITableView单元格时,会发生什么情况是video播放器全屏打开,并且只是说“加载…”整个时间,video不加载,在debugging器中不会报告错误。 variablesmyURL应该等于什么? 任何帮助将不胜感激。

最后算了一下,有两个问题,它只是取名不是完整的文件目录,例如var / mobile …,也有一个video播放器的问题,这是任何人想要做的工作代码和我一样:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"didSelectRowAtIndexPath"); NSString *currentFileName = [filePathsArray[indexPath.row] lastPathComponent]; NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:currentFileName]; //Play the movie now NSURL *videoURL =[NSURL fileURLWithPath:filePath]; MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL]; videoPlayerView.moviePlayer.fullscreen=TRUE; [self presentMoviePlayerViewControllerAnimated:videoPlayerView]; [videoPlayerView.moviePlayer play]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

NSURL与path不同,因为其中包含用于访问资源的协议。 例如http://http://stackoverflow.com

我想问题是,你是从pathstring创build一个URL,试试这个:

 NSString *urlStr = [NSString stringWithFormat:@"file://%@", [filePathsArray objectAtIndex:indexPath.row]]; myURL = [NSURL URLWithString:urlStr];