views:

85

answers:

1

I'm used to creating sounds like this:

NSString *explosionsoundpath = [[NSBundle mainBundle] pathForResource:@"explosion" ofType:@"caf"];
CFURLRef explosionurl = (CFURLRef ) [NSURL fileURLWithPath:explosionsoundpath];
AudioServicesCreateSystemSoundID (explosionurl, &explosion1a);
AudioServicesCreateSystemSoundID (explosionurl, &explosion1b);  

where explosion1a and explosion1b are instance variables declared in the .h file with:

SystemSoundID explosion1a;

Whenever I try to make this process in an array like this

    NSString *plasmasoundpath = [[NSBundle mainBundle] pathForResource:@"plasmasound" ofType:@"caf"];
CFURLRef plasmaurl = (CFURLRef ) [NSURL fileURLWithPath:plasmasoundpath];
SystemSoundID plasmalaunch1;
AudioServicesCreateSystemSoundID (plasmaurl, &plasmalaunch1);
[self.plasmasounds addObject:plasmalaunch1];

I get a warning:

"Passing argument 1 of addObject makes pointer from integer without a cast.

If I put the & symbol before plasmalaunch1 in the addObject argument I get an

incompatible pointer type warning.

I'm trying to create an array of sound effects which I can later play by calling:

SystemSoundID sound = [self.plasmasounds objectAtIndex:i];
AudioServicesPlaySystemSound(sound);

Advice on how to make this work (or a better way to solve this problem) appreciated!

+2  A: 

A SystemSoundID is a integer value, not an object; and a pointer to a number is not a pointer to an object.

You could encapsulate the numeric value in an object before storing it in an NSArray, and then later removing the ID number from the object to play the sound. Or you could store the integer value in a C array instead of an NSArray.

hotpaw2
Thanks for the reply. Can you give an example of how to do either of those? For the later I'm guessing I would take an intValue? I'm not sure how to do the first.
quantumpotato
For the first, you could use the NSNumber numberWithInt: method to convert an int value into an object, and the NSNumber intValue method to get the numeric ID back out of the object.For the latter (C array), you don't need to do anything, as you already have an int value.
hotpaw2
I'd use `NSValue` rather than `NSNumber` for semantic reasons--yes, SystemSoundID is a `typedef` of `int`, but it's not nominally an integer, it's an opaque reference to data. That's all academic though, and either class will work.
Jonathan Grynspan
Got it working - thanks!
quantumpotato