AVAudioPlayer fade volume out

Jonathan  picture Jonathan · Aug 1, 2009 · Viewed 29.2k times · Source

I have an AVAudioPlayer playing some audio (duh!)

The audio is initiated when the user presses a button. When they release it I want the audio to fade out.

I am using Interface builder...so I am trying to hook up a function on "touch up inside" that fades the audio out over 1 sec then stops.

Any ideas?

Thanks

Answer

Bdebeez picture Bdebeez · Nov 3, 2009

Here's how I'm doing it:

-(void)doVolumeFade
{  
    if (self.player.volume > 0.1) {
        self.player.volume = self.player.volume - 0.1;
        [self performSelector:@selector(doVolumeFade) withObject:nil afterDelay:0.1];       
     } else {
        // Stop and get the sound ready for playing again
        [self.player stop];
        self.player.currentTime = 0;
        [self.player prepareToPlay];
        self.player.volume = 1.0;
    }
}

.

11 years later: do note that setVolume#fadeDuration now exists!