This is the final version of the method:
-(void)play:(NSSound *)soundEffect:(BOOL)stopIfPlaying {
BOOL wasPlaying = FALSE;
if([nowPlaying isPlaying]) {
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 25000000;
if(soundEffect == nowPlaying)
{
for(int i = 1; i < 30; ++i)
{
[nowPlaying setVolume: (1.0 / i )];
nanosleep(&ts, &ts);
}
}
[nowPlaying stop];
[nowPlaying setVolume:1];
wasPlaying = TRUE;
}
if(soundEffect != nowPlaying)
{
[soundEffect play];
nowPlaying = soundEffect;
} else if(soundEffect == nowPlaying && ![nowPlaying isPlaying] && !wasPlaying) {
[nowPlaying play];
}
}
Thus, it only disappears if I miss the same sound (that is, I press the same button), I also went to the sediment, and not to sleep, since it has a granularity of 1 second.
I tried my best to understand why my 200 millisecond delay didn’t show any effect, but then 200 NANOseconds really didn’t take so long :-)