0

我希望能够让用户MPMoviePlayerConntroller通过点击我的单元格来选择视频来播放视频,uitableview所以我有这个代码来列出每个单元格中的文件:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
    cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];


    return cell;
}

此代码成功运行,但现在我需要检查用户选择的内容,因此我有此代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSURL * movieURL = [filePathsArray objectAtIndex:indexPath.row];
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
    [[player view] setFrame: [self.view bounds]];  
    [self.view addSubview: [player view]];
    [player play];
}

但是这段代码有一个问题,该行NSURL * movieURL = [filePathsArray objectAtIndex:indexPath.row];不断使模拟器崩溃。

我可能很愚蠢,但我不知道如何解决这个问题。

编辑

问题是filePathsArray它只返回视频文件的名称和扩展名,例如它只会testvideo.mp4在我应该获取该文件的完整路径时显示。有人知道如何解决这个问题吗?

提前致谢...

4

3 回答 3

2

正如您所说,您仅获取文件名;这是因为以下几行:

 filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];

如果你想拥有完整的路径,你应该这样做:

NSString *currentFileName = [filePathsArray[indexPath.row] lastPathComponent];
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:currentFileName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];

//Play the movie now
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: fileURL];
[[player view] setFrame: [self.view bounds]];  
[self.view addSubview: [player view]];
[player play];
于 2013-07-02T10:34:48.743 回答
0

尝试

NSURL * movieURL = [NSURL URLWithString:[[NSString stringWithFormat:@"%@",[filePathsArray objectAtIndex:indexPath.row]]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

或者

NSURL * movieURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[filePathsArray objectAtIndex:indexPath.row]]];
于 2013-07-02T06:52:54.343 回答
0

只需将播放器声明为属性:@property (nonatomic, strong) MPMoviePlayerController *player. 我还建议像这样重写吸气剂:

-(MPMoviewPlayerController *)player {
    if(!_player) {
        _player = [[MPMoviewPlayerController alloc] init];
    }
    return _player
}

然后在 中didSelectRowAtIndexPath,更改如下代码:

NSURL * movieURL = [NSURL URLWithString:[filePathsArray objectAtIndex:indexPath.row]];
self.player.contentURL = movieURL;
[[self.player view] setFrame: [self.view bounds]];  
[self.view addSubview: [self.player view]];
[self.player play];
于 2013-07-02T07:35:54.453 回答