CokePokes
CokePokes

Reputation: 991

How to get filepath of video in UITableView? (tmp directory)

I'm displaying contents of the tmp directory in a UITableView, but cannot get the filepath for each cell.

When the cell is pressed, my app should play the file with MPMoviePlayer but is not.

This is what I have so far:

- (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 gives me the name of the video, but not the path. I feel like I'm over thinking this from lack of sleep.

Upvotes: 0

Views: 246

Answers (3)

Aayushi
Aayushi

Reputation: 797

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)
}

Upvotes: 1

V-Xtreme
V-Xtreme

Reputation: 7333

Change your code like:

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

Upvotes: 1

Midhun MP
Midhun MP

Reputation: 107231

When you use this:

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

It'll return only the file name inside the temporary directory, So use this:

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

NSURL*url = [NSURL fileURLWithPath:movieURL];

Upvotes: 2

Related Questions