views:

55

answers:

1

Hello, guys.

I want to play two sounds, but the last one only after the first ends.

I'm using AudioServicesAddSystemSoundCompletion like that:

  NSArray  *data      = [NSArray arrayWithObjects:target, callback, nil]; 
  NSString *soundPath = [bundle pathForResource:sound ofType:type];

  SystemSoundID soundID;
  AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
  AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, playSoundFinished, data);

  AudioServicesPlaySystemSound(soundID);

and my playSoundFinished method:

static void playSoundFinished(SystemSoundID soundID, void *data) {

    NSArray *data_ = (NSArray *)data;
 id  obj    = [data_ objectAtIndex:0];
 SEL method = NSSelectorFromString([data_ objectAtIndex:1]);

 [obj performSelector:method];

}

The documentation stats that the last parameter must be a void* and that it's the data to work in callback method, but how can I use the NSArray that I pass as argument? I'm new in C and Objective-C, so sorry if it's a dumb question. Casting works fine, but when I try to access the first position, a bad execution occurs. I really didn't understand why void* type. What that means?

In this other post the guy pass a UIButton without any casting and that idea didn't work for me too.

Can you help me?

Thanks in advance.

+1  A: 

You need to retain your NSArray somewhere before passing it to your completion routine as a void*. The OS can't do it because it doesn't even know if a void* is an object or not.

hotpaw2
A void* is simple a placeholder for a pointer to an arbitrary type, e.g. anything, since the routine doesn't know or restrict what type data (object, array, dictionary, struct, int, float, frog, etc.) you want to pass.
hotpaw2
NSArray arrayWithObjects returns a autorelease array. I think that isn't the problem.
R31n4ld0_
An autorelease object isn't retained anywhere (unless you retain it or pass it to something that retains it). Thus non-usable.
hotpaw2
You are right. I had to pass the argument with [data copy] and release it in the callback function. Thanks very much.
R31n4ld0_