从UITableView播放视频 [英] Playing Video From UITableView

查看:157
本文介绍了从UITableView播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My main goal is to be able to click on a table view item and load a video. The table view is populated with the contents of the documents directory and I have been able to do this successfully and add the filename to the cell's label, I have done this with the following code:

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

但是我发现困难的部分是打开文件部分.到目前为止,这是我的代码:

However the part I am finding difficult is opening the file part. This is my code so far:

- (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应该设置为等于什么?任何帮助将不胜感激.

What happens when a user clicks on a UITableView cell is that the video player opens full screen and it just says "loading..." the entire time and the video does not load, no errors are reported either in the debugger. What should the variable myURL be set equal to? Any help would be greatly appreciated.

推荐答案

一段时间后终于搞清楚了,它有两个问题,它只是获取名称而不是完整文件目录. var/mobile ...并且视频播放器也有问题,这是任何想要与我相同的人的有效代码:

Finally figured it out after a while there was two problems it was only fetching the name not the full file directory e.g. var/mobile... and there was a problem with the video player too, this is the working code for anyone wanting to do the same as me:

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

}

这篇关于从UITableView播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆