3

私の主な目標は、テーブル ビュー アイテムをクリックしてビデオをロードできるようにすることです。テーブル ビューにはドキュメント ディレクトリの内容が取り込まれ、これを正常に実行してセルのラベルにファイル名を追加することができました。次のコードを使用してこれを実行しました。

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 を何に等しく設定する必要がありますか? どんな助けでも大歓迎です。

4

2 に答える 2

0

NSURL は、リソースへのアクセスに使用されるプロトコルが含まれているため、パスとは異なります。たとえばhttp://http://stackoverflow.com

問題は、パス文字列から URL を作成していることだと思います。これを試してください。

NSString *urlStr = [NSString stringWithFormat:@"file://%@", [filePathsArray objectAtIndex:indexPath.row]];
myURL = [NSURL URLWithString:urlStr];
于 2014-03-29T23:24:41.337 に答える