The callback method of the AudioServicesAddSystemSoundCompletion method is not called after several calls

I use AudioServicesAddSystemSoundCompletion in my application to determine when the sound is finished, and then trigger another action. For some reason, I get the following behavior: it works for the first 8-12 sounds (at least what I tested), and then the callback defined for AudioServicesAddSystemSoundCompletion is no longer called.

Here is my code for creating sound:

NSString *soundPath = [[NSBundle mainBundle] pathForResource:[[soundFileName componentsSeparatedByString:@"."]objectAtIndex:0]ofType:@"wav"]; Log2(@"soundFileName: %@", soundFileName); CFURLRef soundURL = (CFURLRef)[NSURL fileURLWithPath:soundPath]; AudioServicesCreateSystemSoundID(soundURL, &sound); AudioServicesAddSystemSoundCompletion(sound, nil, nil, playSoundFinished, (void*) self); 

to play sound:

 AudioServicesPlaySystemSound(sound); 

and do some things when the sound has finished playing:

 void playSoundFinished (SystemSoundID sound, void *data) { pf //Typedef for PRETTY_FUNCTION log AudioServicesRemoveSystemSoundCompletion(sound); [AppState sharedInstance].b_touchesFREE = TRUE; if([[AppState sharedInstance].ma_cardsToCheck count] >= 2) { [[AppState sharedInstance].vc_spiel pairFound]; } if (((Card*)data).b_isTurnedFaceUp) { [AppState sharedInstance].i_cardsTurnedFaceUp --; } [(Card*)data release]; } 

Do any of you know why it works the first few times and then stops working?

thanks in advance. Maverick1st

***** Edit ***** I just found out that this happens when I try to play the sound a second time. Maybe I forgot to release it somewhere? But I always thought that AudioServicesRemoveSystemSoundCompletion handles memory management.

***** Another edit ***** So posting this question on Stackoverflow made me think a little deeper about the problem, and I got the solution now (at least I think I got it;)). Unfortunately, I cannot answer the question for myself over the next 7.5 hours, so I need to edit the question.

Just to better understand my problem.

I am programming a memory game, and each Map is a class containing its image front and back and the sound it plays when it turns. since I only initialized the sound when creating the map, I was not sure what I should call AudioServicesRemoveSystemSoundCompletion every time the sound ends.

So, I just tried this without AudioServicesRemoveSystemSoundCompletion, and now it works.

The only thing I'm not sure about is if this could lead to a memory leak or something like that. But for now, this is working fine.

If someone can tell me that this is normal regarding memory usage, I would really be happe. :)

Sincerely. Maverick1st

+4
source share
1 answer

If you create sound (and set sound) only once during the entire life of the application, this should be good in terms of memory management. It is always you create - you let go.

However you must call

 AudioServicesRemoveSystemSoundCompletion(sound); AudioServicesDisposeSystemSoundID(sound); 

when you no longer need the sound (most likely, in the dealloc method of the object on which you created (or save the link) to the sound. Do not change the order of these two, otherwise you have a memory leak.

You may find AppSoundEngine useful. This is a simple wrapper for SystemSoundID and its related C functions.

+5
source

All Articles