【问题标题】:NSBundle pathforResource, where to place movie fileNSBundle pathforResource,放置电影文件的位置
【发布时间】:2011-10-03 22:25:33
【问题描述】:

我不太确定 pathForResource 是如何工作的。我想从我的应用程序播放电影。我将它拖到 XCode 中,选中该框以将其添加到子文件夹并在需要时创建目录。我将它放在一个名为 Videos 的单独文件夹中。这个应用程序没有资源文件夹(有人删除了它,并认为最好只为每种类型的资源(img、vid 等)设置文件夹。所以我不确定将我的电影放在哪里,以及如何放置使用 NSBundle 方法访问它。这是我拥有的,但它似乎无法播放。

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"];  
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];  
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];  
    [moviePlayerController.view setFrame:CGRectMake(120, 120, 300, 200)];
    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(moviePlaybackComplete:)  
                                                 name:MPMoviePlayerPlaybackDidFinishNotification  
                                               object:moviePlayerController];  
    [self.view addSubview:moviePlayerController.view];  
    moviePlayerController.fullscreen = YES;  
    [moviePlayerController play];  

谢谢。

【问题讨论】:

    标签: iphone nsbundle


    【解决方案1】:

    此代码应列出 mainBundle 中的所有内容

    NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
    NSArray *files = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.m4v'"]];
    NSLog(@"Directory Start");
    NSLog(@"%@",dirContents);
    NSLog(@"Directory End");
    NSLog(@"files: %@",files);
    

    或者你可以直接去 ~user/Library/Application Support/iPhone Simulator 找到你的应用并右键单击它并显示内容以查看你的应用的目录结构。

    【讨论】:

    • 如果我可以 NSLog 文件路径,那么我假设我的路径是正确的吗?否则 NSString 将为零对吗?所以一定是我在播放电影时做错了什么?谢谢。
    • 你应该输入一个if语句来检查文件是否存在。 if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) { // Check to see if file exist // do something }
    猜你喜欢
    • 1970-01-01
    • 2011-04-26
    • 2011-09-09
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多