在目录中搜索具有特定文件扩展名的文件,然后将它们添加到数组中,然后运行其中一个
我对 Objective-C 和 Mac 不太熟悉,我正在尝试制作一个随机视频播放器。
我使用 Java 为 Windows 制作了它,但我想制作一个本机 Mac 应用程序来为我做这件事。基本上,该程序有一个目录选择器和一个按钮,您选择要搜索的目录,然后单击该按钮,就会播放随机视频。
所以我需要知道的是,如何在指定目录中搜索具有某些扩展名(avi、mp4、mkv 等)的文件,然后将它们的路径添加到数组中?然后扫描完成后随机生成一个数字,然后打开数组中与该位置关联的文件。我希望能够通过目录递归搜索。
感谢您的帮助。
仅供参考:我将其用于我的电视节目文件夹,每个节目都有一个不同的文件夹,然后每个节目季都有一个子文件夹。
I'm new to Objective-C and Macs in general and i'm trying to make a Random Video Player.
I made it for windows using Java but I want to make a native mac app to do it for me. Basically the program has a directory chooser and a button, you select which directory you want to search and then click the button and a random video plays.
So what I need to know is, how do I search the specified directory for files with certain extensions (avi, mp4, mkv etc.) and then add their paths to an array ? and then after the scan is finished randomly generate a number, and then open the file associated with that location in the array. I would like to be able to search recursively through the directory.
Thanks for the help.
Just an FYI : I use this for my TV Shows folder, I have a different folder for each show and then sub folders for each season of the show.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,您可能想要:
使用 NSOpenPanel。 (相应地设置 setCanChooseDirectories 和 setCanChooseFiles。)
使用 NSFileManager。 enumeratorAtURL:includePropertiesForKeys:options:errorHandler: 方法可能就是您所追求的,因为它将列出所有子目录的内容等。
迭代文件,放置 URL在 NSMutableArray 在选择随机条目之前进行操作。
使用 NSWorkspace 打开文件(请参阅 openFile 方法等)
顺便说一句,如果您需要示例代码,则大部分 Apple 类引用上面链接的文档附有示例代码。 (请参阅每个文档标题中的“相关示例代码”部分。)
OK, well you're probably going to want to:
Select a directory using an NSOpenPanel. (Set setCanChooseDirectories and setCanChooseFiles accordingly.)
Get the contents of that directory (well, URL) using an NSFileManager. The enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: method is probably what you're after as it'll list the contents of all sub-dirs, etc.
Iterate over the files, putting the URLs in an NSMutableArray as you go before picking a random entry.
Use an NSWorkspace to open the file (see the openFile method, etc.)
Incidentally, if you're after sample code most of the Apple class reference documentation linked to above have sample code attached. (See the "Related sample code" section in the header of each document.)