Unable to put NSSound instance in NSMutableArray ion cocoa

125 Views Asked by At

I'm having trouble adding NSsound instances into an NSMutableArray The error I get is that I cannot do [soundFiles addObject:soundObj], because soundObj is nil. But if I do [soundObj play], it will play - so it is an instance.

//Name of all the sounds to load in.
sounds = [NSArray arrayWithObjects:
      @"sound1",
      @"sound2",
      nil];


for (int i = 0; i < sounds.count; i++) {
    NSString *soundName = [NSString stringWithString:sounds[i]];
    NSSound *soundObj = [NSSound soundNamed:soundName];
    [soundFiles addObject:soundObj];
}

If I change [soundFiles addObject:soundObj] to [soundFiles addObject:soundName] it is fine, so it is something with trying to pass the NSSound

I'm hoping to be able to preload a bunch of very short sounds to make them play the minute they are called. milliseconds matter for this project.

I was hoping this would allow me to do [[soundFiles objectAtIndex:1] play] - as I was thinking that would result it being faster than creating the NSSound object when it is time to play it.

1

There are 1 best solutions below

0
On

You've probably figured this out by now, but my guess is that you were writing the code before you had created ALL the sound files you wanted to work with. So when you tried to create an NSSound instance of those nonexistent files, it got set to nil. It's hard to troubleshoot because NSSound doesn't throw an error when you point it to a non-existent file.

I had the identical problem until I created all the sound files I wanted to use. I just wanted to post my answer to help anyone else who's having this problem because a Google search turns up nothing that helps.