Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would wrap the sound into a subclass of <code>NSObject</code> and make it a self contained unit. A sound would have operations like <code>play</code>, <code>stop</code>, <code>pause</code>, and accessors like <code>isPlaying</code>, etc.</p> <p>Then to make it even more generic I would search for all files matching the pattern <code>"*.wav"</code> and then for each matched file name, initialize a <code>Sound</code> object with that file name, and add it to an array.</p> <p>Here's what I imagine the Sound object would look like:</p> <pre><code>@interface Sound : NSObject @property FMOD::Sound *sound; - (id)initWithSoundFilePath:(NSString *)path; - (void)play; - (void)stop; @end @implementation Sound - (void)dealloc { // free the memory occupied by the sound pointer here } - (id)initWithSoundFilePath:(NSString *)path { self = [super init]; if (self) { result = system-&gt;createSound(path, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &amp;sound); ERRCHECK(result); } return self; } - (void)play { FMOD_RESULT result = FMOD_OK; result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound, false, /* What is this wob? */); ERRCHECK(result); } - (void)stop { FMOD_RESULT result = FMOD_OK; result = /* What is this wob */-&gt;stop(); ERRCHECK(result); } @end </code></pre> <p>So there you have it. A sound is nicely encapsulated now. I found this <a href="https://stackoverflow.com/questions/499673/getting-a-list-of-files-in-a-directory-with-a-glob">answer</a> helpful in finding a list of all files in a certain directory matching some criteria. You could use that in your view controller to automatically generate all relevant Sound objects and add it to an array.</p> <pre><code>- (NSArray *)getPathsOfSoundFiles { NSString *rootPath = [[NSBundle mainBundle] resourcePath]; NSFileManager *fm = [NSFileManager defaultManager]; NSArray *files = [fm contentsOfDirectoryAtPath:rootPath error:nil]; NSPredicate *soundFileFilter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.wav'"]; NSArray *soundFilePaths = [files filteredArrayUsingPredicate:soundFileFilter]; return soundFilePaths; } </code></pre> <p>Ok, now that you can retrieve the paths to all .wav files, the next step is to initialize them in your <code>viewWillAppear</code> or whatever method makes most sense.</p> <pre><code>- (void)viewWillAppear:(BOOL)animated { NSArray *paths = [self getPathsOfSoundFiles]; NSMutableArray *sounds = [NSMutableArray array]; for (NSString *path in paths) { Sound *sound = [[Sound alloc] initWithSoundFilePath:path]; [sounds addObject:sound]; } self.sounds = sounds; } </code></pre> <p>And with the sounds array setup, playing and stopping a given sound becomes rather easy. Use could create a method that takes an index into the array, or maybe a <code>Sound</code> object itself and does the job.</p> <pre><code>- (void)playSoundAtIndex:(NSUInteger)soundIndex { Sound *sound = [self.sounds objectAtIndex:soundIndex]; [sound play]; } - (void)stopSoundAtIndex:(NSUInteger)soundIndex { Sound *sound = [self.sounds objectAtIndex:soundIndex]; [sound stop]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload