如何在UITableView中获取视频的文件路径? (tmp目录)

时间:2013-01-15 05:58:53

标签: ios objective-c uitableview mpmovieplayercontroller

我在UITableView中显示tmp目录的内容,但无法获取每个单元格的文件路径。

当按下单元格时,我的应用程序应该使用MPMoviePlayer播放该文件但不是。

这是我到目前为止所做的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


NSString *tmpDirectory = NSTemporaryDirectory();
fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory error:nil]; //nsarray

NSString *movieURL = [fileList objectAtIndex:indexPath.row];

NSURL *url = [NSURL URLWithString:movieURL];

_moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

[self presentModalViewController:_moviePlayerViewController animated:YES];
}

NSlog为我提供了视频的名称,但不是路径。我觉得我因为睡眠不足而忽略了这一点。

3 个答案:

答案 0 :(得分:2)

使用时:

NSString *movieURL = [fileList objectAtIndex:indexPath.row];

它只返回临时目录中的文件名, 所以使用这个:

NSString *movieURL = [tmpDirectory stringByAppendingPathComponent:[fileList objectAtIndex:indexPath.row]];

NSURL*url = [NSURL fileURLWithPath:movieURL];

答案 1 :(得分:1)

更改您的代码,如:

    NSString *tmpDirectory = NSTemporaryDirectory();
    NSLog(@"%@",tmpDirectory);
    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory error:nil]; //nsarray

    NSString *movieURL = [fileList objectAtIndex:indexPath.row];
    NSString *path=[tmpDirectory stringByAppendingPathComponent:movieURL];

答案 2 :(得分:1)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let tmpDirectory: String = NSTemporaryDirectory()
    print("\(tmpDirectory)")
    var fileList: [Any]? = try? FileManager.default.contentsOfDirectory(atPath: tmpDirectory)
    //nsarray
    let movieURL: String? = (fileList?[indexPath.row] as? String)
    let path: String = URL(fileURLWithPath: tmpDirectory).appendingPathComponent(movieURL!).absoluteString
    print(path)
}