简体   繁体   中英

Search a directory for files with a certain file extensions and then add them to an array, then run one of them

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.

OK, well you're probably going to want to:

  1. Select a directory using an NSOpenPanel . (Set setCanChooseDirectories and setCanChooseFiles accordingly.)

  2. 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.

  3. Iterate over the files, putting the URLs in an NSMutableArray as you go before picking a random entry.

  4. 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM