3

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

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 单元格时会发生什么情况,视频播放器会全屏打开,并且一直说“正在加载...”并且视频没有加载,调试器中也没有报告错误。应该将变量 myURL 设置为等于什么?任何帮助将不胜感激。

4

2 回答 2

3

过了一会儿终于弄清楚了,有两个问题,它只是获取名称而不是完整的文件目录,例如 var/mobile ......而且视频播放器也有问题,这是任何想要做的人的工作代码和我一样:

- (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];

}
于 2014-03-30T10:57:28.550 回答
0

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

我想问题是你正在从路径字符串创建一个 URL,试试这个:

NSString *urlStr = [NSString stringWithFormat:@"file://%@", [filePathsArray objectAtIndex:indexPath.row]];
myURL = [NSURL URLWithString:urlStr];
于 2014-03-29T23:24:41.337 回答