使用文档目录中的音频文件填充UIPickerView

时间:2013-03-01 05:56:25

标签: ios nsarray uipickerview

我试图从目录中获取.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);

有人可以帮我这个吗?

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

1 个答案:

答案 0 :(得分: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方法。