0

我正在尝试从目录中获取 .caf 文件,然后将它们放入 UIPickerView 中,以便在点击每一行时播放它们。

到目前为止,我有这段代码,但不知道如何将文件放入数组中。

NSString *extension = @"caf";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentsDirectory14 = [documentsDirectory stringByAppendingPathComponent:@"/Family"];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory14 error:&error];
NSMutableArray *cafFiles = [NSMutableArray arrayWithCapacity: [contents count]];

NSString *filename;
for (filename in contents)

    NSLog(@"filename;%@",filename);

{
    if ([[filename pathExtension] isEqualToString:extension])
    {

        [cafFiles  addObject: [NSMutableArray  arrayWithContentsOfFile:[documentsDirectory14 stringByAppendingPathComponent:filename]]];
    }
}    

 NSLog(@"jpegFiles;%@",cafFiles );

covers= [NSMutableArray arrayWithArray:cafFiles];{
    for (filename in cafFiles);
}
NSLog(@"%@", covers);

有人可以帮我吗?

如果我使用封面作为它的数组,这会将文件的名称放在选择器视图中吗?

4

1 回答 1

0

看起来您正在尝试加载实际的音频数据,而选择器视图不需要这些数据 - 您想要文件的名称。试试这个代码,它是基于你的:

NSString *extension = @"caf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *familyDirectory = [paths[0] stringByAppendingPathComponent:@"Family"];
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:familyDirectory
                                                                        error:NULL];
NSIndexSet *cafFileNameIndexes = [directoryContents indexesOfObjectsPassingTest:^BOOL(NSString *fileName, NSUInteger idx, BOOL *stop) {
    return [[fileName pathExtension] isEqualToString:extension];
}];
NSArray *cafFileNames = [directoryContents objectsAtIndexes:cafFileNameIndexes];

然后cafFileNames用作底层数据来实现你的UIPickerViewDelegate/UIPickerViewDataSource方法。

于 2013-03-01T09:07:14.967 回答