Search code examples
iosopenalfinch

Fading out sounds when using finch in iOS


I am using the Finch openAL wrapper in iOS and would like to fade out my FISound's.

Suppose I have a 30 second sound, I would like to be able to fade out the sound over 5 seconds after 15 seconds for example.

I'd like to avoid dropping down to openAL for it if possible.


Solution

  • Set up an NSTimer that repeatedly decreases the sound gain until zero. Or you can do it like this:

    static const float FadeStep = 0.1;
    static const NSTimeInterval FadeDelay = 0.1;
    
    @implementation FISound
    
    - (void) fadeOut
    {
        self.gain = MAX(0, self.gain - FadeStep);
        if (self.gain > 0) {
            [self performSelector:_cmd afterDelay:FadeDelay withObject:nil];
        }
    }
    
    @end
    

    This is a quick and dirty solution, but it should work fine for a lot of cases.