0

我在 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 给了我视频的名称,但不是路径。我觉得我因为睡眠不足而过度思考这个问题。

4

3 回答 3

2

当你使用这个:

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

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

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

NSURL*url = [NSURL fileURLWithPath:movieURL];
于 2013-01-15T06:09:30.007 回答
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];
于 2013-01-15T06:16:31.517 回答
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)
}
于 2017-04-08T11:31:27.753 回答