views:

1721

answers:

4

Hey people,

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

A: 

ok

I thought to set up a 1 sec timer loop and fade the volume by incrementally by 1 each loop.

but how do I do I minus the current volume by one?

pPlayer09.volume = ?

thanks

Jonathan
A: 
NSInt volume = pPlayer09.volume;
if (volume > 0)
{
volume--;
}
pPlayer09.volume = volume;

There's probably a more efficient way, but that's the first thing I came up with.

chedabob
A: 

How about this: (if time passed in is negative then fade out the sound, otherwise fade in)

- (void) fadeInOutVolumeOverTime: (NSNumber *)time
{
#define fade_out_steps  0.1
    float   theVolume = player.volume;
    NSTimeInterval theTime = [time doubleValue];
    int    sign = (theTime >= 0) ? 1 : -1;

// before we call this, if we are fading out, we save the volume
// so that we can restore back to that level in the fade in
    if ((sign == 1) &&
      ((theVolume >= savedVolume) ||
          (theTime == 0))) {
     player.volume = savedVolume;
    }
    else if ((sign == -1) && (theVolume <= 0)) {
     NSLog(@"fading");
     [player pause];
     [self performSelector:@selector(fadeInOutVolumeOverTime:) withObject:[NSNumber numberWithDouble:0] afterDelay:1.0];

    }
    else {
     theTime *= fade_out_steps;
     player.volume = theVolume + fade_out_steps * sign;
     [self performSelector:@selector(fadeInOutVolumeOverTime:) withObject:time afterDelay:fabs(theTime)];
    }
}
mahboudz
+3  A: 

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;
    }
}
Bdebeez