【问题标题】:Playing Video From UITableView从 UITableView 播放视频
【发布时间】:2014-05-09 03:58:20
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: ios objective-c uitableview mpmediaplayercontroller


    【解决方案1】:

    NSURL 与路径不同,因为它包含用于访问资源的协议。 比如http://stackoverflow.com中的http://

    我猜问题是你正在从路径字符串创建一个 URL,试试这个:

    NSString *urlStr = [NSString stringWithFormat:@"file://%@", [filePathsArray objectAtIndex:indexPath.row]];
    myURL = [NSURL URLWithString:urlStr];
    

    【讨论】:

    • 不幸的是它不起作用,但无论如何感谢您的帮助!如果您有任何其他想法,请告诉我,我已经被这个问题发疯了。
    【解决方案2】:

    一段时间后终于弄清楚了,有两个问题,它只是获取名称而不是完整的文件目录,例如var/mobile...而且视频播放器也有问题,这是任何想和我一样的人的工作代码:

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

    【讨论】:

    • moviePlayer 是 .h 文件中的一个属性,它是:@property(nonatomic, readonly) MPMoviePlayerController *moviePlayer;
    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 2012-04-01
    相关资源
    最近更新 更多