【问题标题】:Objective-C: How to play a video from a UITableViewCell?Objective-C:如何从 UITableViewCell 播放视频?
【发布时间】:2013-07-05 14:00:07
【问题描述】:

下面我有一些代码,一旦用户在uitableview 中按名称选择了他想要的视频,就应该播放视频:

    NSLog(@"url %@",[filePathsArray objectAtIndex:indexPath.row]);

  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
movieController = [[MPMoviePlayerController alloc] initWithContentURL: fileURL];
[[movieController view] setFrame: [self.view bounds]];
[self.view addSubview: [movieController view]];
[movieController play];

代码应该采用filePathsArray,这只是我的文档目录中的文件名,而且我不能只使用文件名来播放视频,它必须是完整路径。例如,我需要像在模拟器中一样获得完整路径。 “/Users/UserName/Library/Application Support/iPhone Simulator/4.3.2/Applications” 而我得到的只是文件名,例如“123.mp4”。

代码的问题是这一行:NSString *currentFileName = [filePathsArray[indexPath.row] lastPathComponent]; 再次是由于刚刚接收到文件名。该问题导致模拟器崩溃,我收到以下信息:

Thread 1: EXC_BAD_ACCESS(code=1, address=0xc08384e7)

有人可以帮忙吗?

编辑

我放了NSLog(@"row %i - filePathsArray %@", indexPath.row, filePathsArray); 在我的其他代码所在的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {} 中,我再次收到线程错误,但我随后将NSLog(@"row %i - filePathsArray %@", indexPath.row, filePathsArray); 放入

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

以及调试器输出中显示的内容:

filePathsArray (
    "1372954494.023550.mp4",
    "1372954525.440379.mp4",
    "1372954698.070642.mp4",
    "1372954717.124080.mp4",
    "1372954881.568860.mp4",
    "1372954995.932892.mp4",
    "1372955079.400627.mp4",
    "1372955121.735828.mp4",
    "1372955151.191822.mp4",
    "1372955204.253028.mp4",
    "1372955317.622774.mp4",
    "1372955394.617367.mp4",
    "1372955455.130194.mp4",
    "1372955748.579562.mp4",
    "1372955801.431035.mp4",
    "1372955982.626679.mp4",
    "1372956220.934516.mp4",
    "1372956313.205083.mp4",
    "1372956396.179499.mp4",
    "1372956433.715887.mp4",
    "1372956521.383647.mp4",
    "1372956556.635456.mp4",
    "1372956865.871915.mp4",
    "1372957260.725387.mp4",
    "1372957729.062718.mp4",
    "1372957817.498707.mp4",
    "1372958542.838564.mp4",
    "1372958898.002360.mp4",
    "1372958944.864006.mp4",
    "1372958981.295354.mp4",
    "1372959288.333242.mp4",
    "1372959511.324370.mp4",
    "1372959557.161786.mp4",
    "1372959979.054552.mp4",
    "1372960381.754023.mp4",
    "1372960609.649583.mp4",
    "1372964668.874020.mp4",
    "1372964985.709783.mp4",
    "1372965088.565757.mp4",
    "1372965163.876937.mp4",
    "1372965296.464444.mp4",
    "1373027973.427050.mp4",
    "1373028300.018729.mp4",
    "1373028402.868729.mp4",
    "1373031752.143091.mp4"
)

这是我保存文件的所有正确名称。

编辑

这是我的完整代码:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(!filePathsArray)
    {
        return 0;
    }
    if ([filePathsArray count] > 0)
        return [filePathsArray count];
    else
        return 0;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    filePathsArray = [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil]mutableCopy];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"row %i - filePathsArray %@", indexPath.row, filePathsArray);

    NSString *currentFileName = filePathsArray[indexPath.row];
 NSLog(@"url %@",[filePathsArray objectAtIndex:indexPath.row]);

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

 //Play the movie now
   movieController = [[MPMoviePlayerController alloc] initWithContentURL: fileURL];
 [[movieController view] setFrame: [self.view bounds]];
   [self.view addSubview: [movieController view]];
   [movieController play];
}

-(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];
    NSLog(@"urlstring %@",[filePathsArray objectAtIndex:indexPath.row]);

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

    return cell;
}

【问题讨论】:

  • 第一行 NSLog 的输出是什么?真的有路径还是只有文件名?如果只是文件名,则必须将文件路径更改为: NSString *currentFileName = filePathsArray[indexPath.row];
  • @Swissdude 这只是 filePathArray 的文件名
  • @Swissdude 并且您的代码仍然导致崩溃
  • 您的电影真的在您应用的文档文件夹中吗? fileURL 的 NSLog 说明了什么?确保路径正确并且电影确实在预期的位置。此外,模拟器不区分大小写,而实际设备区分大小写。所以 «Movie.mp4» 会在模拟器上找到 «movie.mp4» 而不是在设备上。
  • 好的,我很确定你没有正确初始化你的数组。在您的 viewDidLoad 方法中尝试: filePathsArray = [[NSArray alloc] initWithArray: [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy]]; - 我认为您在 .h 文件中添加了一个名为 NSArray *filePathsArray 的属性?

标签: ios objective-c mpmovieplayercontroller


【解决方案1】:

根据要求,解决方案如下:

在 viewDidLoad 方法中,你必须分配数组:

filePathsArray = [[NSArray alloc] initWithArray: [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 2013-03-07
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多