AVAudioPlayer fade volume out

前端 未结 13 2058
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 12:07

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

相关标签:
13条回答
  • 2020-12-12 12:33

    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)];
        }
    }
    
    0 讨论(0)
提交回复
热议问题