AVAudioPlayer fade volume out
Asked Answered
N

13

51

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

Noninterference answered 1/8, 2009 at 13:24 Comment(3)
Do note that setVolume#fadeDuration: now exists - so there's no need for this very old question!Calcimine
Do note that for AVPlayer (not AVAudioPlayer), there's this sort of approach: gist.github.com/artem-sherbachuk/…Calcimine
@Calcimine I tried that and it doesn't work in Swift 5. I'm not sure if I'm doing something wrong.Apollus
C
128

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!

Connell answered 3/11, 2009 at 14:10 Comment(8)
Why isn't this marked as the answer yet? It's been almost 4 years!Miscarriage
Speaking from experience: Make sure you've got AudioToolbox and not just AVFoundation added under "Link Binary With Libraries", or you will tear your hair out trying to figure out why the audio isn't actually fading even though your NSLogs are showing this method is getting called and the volume is changing. Otherwise, yes, 100% this.Microprint
I made a category out of this. Thanks for the post.Whitacre
Does anyone have any idea how to do this in Swift?Niles
@skyguy this is the same thing but in swift https://mcmap.net/q/344317/-avaudioplayer-fade-volume-outJambalaya
why not self.player.volume >= 0.1 ?Carborundum
This is what I'm doing in my app, but sometimes it's a little jitteryDisburden
Please do not use this very old code, which "jumps by .1 each time", it's crazy.Calcimine
H
42

Swift has an AVAudioPlayer method you can use for fading out which was included as of iOS 10.0:

var audioPlayer = AVAudioPlayer()
...
audioPlayer.setVolume(0, fadeDuration: 3)
Hardset answered 1/12, 2016 at 10:35 Comment(3)
Does this stop the audio after the volume gets set to 0? or is it still playing but with 0 volume?Homogenous
It doesn't stop the audio player.Impassive
Very sadly, AVPlayer does not have such a feature :/Calcimine
T
15

I tackled this problem using an NSOperation subclass so fading the volume doesn't block the main thread. It also allows fades to be queued and and forgotten about. This is especially useful for playing one shot sounds with fade-in and fade-out effects as they are dealloced after the last fade is completed.

// Example of MXAudioPlayerFadeOperation in NSOperationQueue 
 NSOperationQueue *audioFaderQueue = [[NSOperationQueue alloc] init];
  [audioFaderQueue setMaxConcurrentOperationCount:1]; // Execute fades serially.

  NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bg" ofType:@"mp3"]; // path to bg.mp3
  AVAudioPlayer *player = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:NULL] autorelease];
  [player setNumberOfLoops:-1];
  [player setVolume:0.0];

  // Note that delay is delay after last fade due to the Operation Queue working serially.
  MXAudioPlayerFadeOperation *fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:player toVolume:1.0 overDuration:3.0];
  [fadeIn setDelay:2.0];
  MXAudioPlayerFadeOperation *fadeDown = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:player toVolume:0.1 overDuration:3.0];
  [fadeDown setDelay:0.0];
  MXAudioPlayerFadeOperation *fadeUp = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:player toVolume:1.0 overDuration:4.0];
  [fadeUp setDelay:0.0];
  MXAudioPlayerFadeOperation *fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:player toVolume:0.0 overDuration:3.0];
  [fadeOut setDelay:2.0];

  [audioFaderQueue addOperation:fadeIn]; // 2.0s - 5.0s
  [audioFaderQueue addOperation:fadeDown]; // 5.0s - 8.0s
  [audioFaderQueue addOperation:fadeUp]; // 8.0s - 12.0s
  [audioFaderQueue addOperation:fadeOut]; // 14.0s - 17.0s

  [fadeIn release];
  [fadeDown release];
  [fadeUp release];
  [fadeOut release];

For MXAudioPlayerFadeOperation class code see this post.

Turbit answered 1/12, 2010 at 0:58 Comment(2)
That link is dead. Does anyone have a copy hosted anywhere else?Reareace
That original code is long gone however it's been improved on and used elsewhere on stack overflow #12436349Turbit
S
10

I ended up combining some of the answer together and converted it to Swift ending up in this method:

func fadeVolumeAndPause(){
    if self.player?.volume > 0.1 {
        self.player?.volume = self.player!.volume - 0.1

        var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
        dispatch_after(dispatchTime, dispatch_get_main_queue(), {
            self.fadeVolumeAndPause()
        })

    } else {
        self.player?.pause()
        self.player?.volume = 1.0
    }
}
Spellbind answered 2/3, 2015 at 12:43 Comment(1)
Very helpful. Thanks. I replaced the dispatchTime variable and dispatch_after function with: DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { }Ezaria
R
8

These are all good answers, however they don't deal with specifying the rate of fade (or applying a logarithmic curve to the fade, which is sometimes desirable), or specifying the number of dB's reduction from unity that you are fading to.

this is an except from one of my apps, with a few "bells and whistles" removed, that are not relevant to this question.

enjoy!

#define linearToDecibels(linear) (MIN(10,MAX(-100,20.0 * log10(linear))))
#define decibelsToLinear(decibels) (pow (10, (0.05 * decibels)))

#define fadeInfoId(n) [fadeInfo objectForKey:@#n]
#define fadeInfoObject(NSObject,n) ((NSObject*) fadeInfoId(n))
#define fadeInfoFloat(n) [fadeInfoId(n) floatValue]
#define useFadeInfoObject(n) * n = fadeInfoId(n)
#define useFadeInfoFloat(n) n = fadeInfoFloat(n)
#define setFadeInfoId(n,x) [fadeInfo setObject:x forKey:@#n]
#define setFadeInfoFloat(n,x) setFadeInfoId(n,[NSNumber numberWithFloat:x])
#define setFadeInfoFlag(n) setFadeInfoId(n,[NSNumber numberWithBool:YES])

#define saveFadeInfoId(n) setFadeInfoId(n,n)
#define saveFadeInfoFloat(n) setFadeInfoFloat(n,n)

#define fadeAVAudioPlayer_default           nil
#define fadeAVAudioPlayer_linearFade        @"linearFade"
#define fadeAVAudioPlayer_fadeToStop        @"fadeToStop"
#define fadeAVAudioPlayer_linearFadeToStop  @"linearFadeToStop"





-(void) fadeAVAudioPlayerTimerEvent:(NSTimer *) timer {
    NSMutableDictionary *fadeInfo = timer.userInfo;
    NSTimeInterval elapsed = 0 - [fadeInfoObject(NSDate,startTime) timeIntervalSinceNow];
    NSTimeInterval useFadeInfoFloat(fadeTime);
    float          useFadeInfoFloat(fadeToLevel);
    AVAudioPlayer  useFadeInfoObject(player);
    double linear;
    if (elapsed>fadeTime) {

        if (fadeInfoId(stopPlaybackAtFadeTime)) {
            [player stop];
            linear = fadeInfoFloat(fadeFromLevel);

        } else {

            linear = fadeToLevel;
        }
        [timer invalidate];
        [fadeInfo release];

    } else {


        if (fadeInfoId(linearCurve)) {
            float useFadeInfoFloat(fadeFromLevel);
            float fadeDelta = fadeToLevel-fadeFromLevel;
            linear = fadeFromLevel + (fadeDelta * (elapsed/fadeTime));
        } else {
            float useFadeInfoFloat(fadeToDB);
            float useFadeInfoFloat(fadeFromDB);

            float fadeDelta = fadeToDB-fadeFromDB;
            float decibels = fadeFromDB + (fadeDelta * (elapsed/fadeTime));
            linear = decibelsToLinear(decibels);
        }       
    }

    [player setVolume: linear];

    //[self displayFaderLevelForMedia:player];
    //[self updateMediaVolumeLabel:player];
}


-(void) fadeAVAudioPlayerLinear:(AVAudioPlayer *)player over:(NSTimeInterval) fadeTime fadeToLevel:(float) fadeToLevel fadeMode:(NSString*)fadeMode {
    NSMutableDictionary *fadeInfo = [[NSMutableDictionary alloc ]init];
    saveFadeInfoId(player);
    float fadeFromLevel = player.volume;// to optimize macros put value in var, so we don't call method 3 times.
    float fadeFromDB = linearToDecibels(fadeFromLevel);
    float fadeToDB   = linearToDecibels(fadeToLevel);

    saveFadeInfoFloat(fadeFromLevel);
    saveFadeInfoFloat(fadeToLevel);
    saveFadeInfoFloat(fadeToDB);
    saveFadeInfoFloat(fadeFromDB);
    saveFadeInfoFloat(fadeTime);

    setFadeInfoId(startTime,[NSDate date]);
    if([fadeMode isEqualToString:fadeAVAudioPlayer_fadeToStop]||[fadeMode isEqualToString:fadeAVAudioPlayer_linearFadeToStop]){ 
        setFadeInfoFlag(stopPlaybackAtFadeTime);
    }
    if([fadeMode isEqualToString:fadeAVAudioPlayer_linearFade]||[fadeMode isEqualToString:fadeAVAudioPlayer_linearFadeToStop]){ 
        setFadeInfoFlag(linearCurve);
    }

    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(fadeAVAudioPlayerTimerEvent:) userInfo:fadeInfo repeats:YES];
}

-(void) fadeAVAudioPlayer:(AVAudioPlayer *)player over:(NSTimeInterval) fadeTime fadeToDB:(float) fadeToDB fadeMode:(NSString*)fadeMode {
    [self fadeAVAudioPlayerLinear:player over:fadeTime fadeToLevel:decibelsToLinear(fadeToDB) fadeMode:fadeMode ];
}

-(void) fadeoutAVAudioPlayer:(AVAudioPlayer *)player {
    [self fadeAVAudioPlayerLinear:player over:5.0 fadeToLevel:0 fadeMode:fadeAVAudioPlayer_default];
}

-(void) fadeinAVAudioPlayer:(AVAudioPlayer *)player {
    [self fadeAVAudioPlayerLinear:player over:5.0 fadeToLevel:0 fadeMode:fadeAVAudioPlayer_default];
}
Rambow answered 29/9, 2011 at 10:44 Comment(0)
B
5

An extension for swift 3 inspired from the most voted answer. For those of you who like copy-pasting :)

extension AVAudioPlayer {
    func fadeOut() {
        if volume > 0.1 {
            // Fade
            volume -= 0.1
            perform(#selector(fadeOut), with: nil, afterDelay: 0.1)
        } else {
            // Stop and get the sound ready for playing again
            stop()
            prepareToPlay()
            volume = 1
        }
    }
}
Berglund answered 28/9, 2016 at 14:0 Comment(2)
Although this sure works (and I love all copy paste :) ) , really, if you want perfection just use a display link and smooth it out that way :/ (rather than .1 chunks)Calcimine
do note that setVolume#fadeDuration exists now, so this is all academic Unless you're using AVPlayer :/Calcimine
A
4

I wrote a helper class in Swift for fading AvAudioPlayer in and out. You can use logarithmic volume function for more gradual fading effect.

let player = AVAudioPlayer(contentsOfURL: soundURL, error: nil)

let fader = iiFaderForAvAudioPlayer(player: player)
fader.fadeIn()
fader.fadeOut()

Here a demo app: https://github.com/evgenyneu/sound-fader-ios

Aulic answered 31/12, 2014 at 9:37 Comment(0)
A
4

Swift 3

I like Ambroise Collon answer's , so i voted up but Swift is statically typed so the performSelector: methods are to fall by the wayside, maybe an alternative could be dispatch async (in this version I've added also the destination volume as parameter)

func dispatchDelay(delay:Double, closure:@escaping ()->()) {
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay, execute: closure)
}

extension AVAudioPlayer {
    func fadeOut(vol:Float) {
        if volume > vol {
            //print("vol is : \(vol) and volume is: \(volume)")
            dispatchDelay(delay: 0.1, closure: {
                [weak self] in
                guard let strongSelf = self else { return }
                strongSelf.volume -= 0.01
                strongSelf.fadeOut(vol: vol)
            })
        } else {
            volume = vol
        }
    }
    func fadeIn(vol:Float) {
        if volume < vol {
            dispatchDelay(delay: 0.1, closure: {
                [weak self] in
                guard let strongSelf = self else { return }
                strongSelf.volume += 0.01
                strongSelf.fadeIn(vol: vol)
            })
        } else {
            volume = vol
        }
    }
}
Ascensive answered 1/10, 2016 at 17:2 Comment(0)
C
2

This seems to me to be a descent use of an NSOperationQueue.

Hence here is my solution:

-(void) fadeIn
{
    if (self.currentPlayer.volume >= 1.0f) return;
    else {
        self.currentPlayer.volume+=0.10;
        __weak typeof (self) weakSelf = self;
        [NSThread sleepForTimeInterval:0.2f];
        [self.fadingQueue addOperationWithBlock:^{
            NSLog(@"fading in %.2f", self.currentPlayer.volume);
            [weakSelf fadeIn];
        }];
    }
}
-(void) fadeOut
{
    if (self.currentPlayer.volume <= 0.0f) return;
    else {
        self.currentPlayer.volume -=0.1;
        __weak typeof (self) weakSelf = self;
        [NSThread sleepForTimeInterval:0.2f];
        [self.fadingQueue addOperationWithBlock:^{
            NSLog(@"fading out %.2f", self.currentPlayer.volume);
            [weakSelf fadeOut];
        }];
    }
}
Couch answered 11/5, 2014 at 14:13 Comment(1)
Obviously this is redundant and can be refactored a bit to use just one method. But its illustrative nonetheless.Couch
T
1

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)];
    }
}
Torey answered 3/8, 2009 at 9:55 Comment(0)
S
1

In Objective-C try this:

NSURL *trackURL = [[NSURL alloc]initFileURLWithPath:@"path to audio track"];

// fade duration in seconds
NSTimeInterval fadeDuration = 0.3;

// duration of the audio track in seconds
NSTimeInterval audioTrackDuration = 5.0; 

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]  initWithContentsOfURL:trackURL error:nil];
[audioPlayer play];

// first we set the volume to 1 - highest
[audioPlayer setVolume:1.0]; 

// then to 0 - lowest
[musicAudioPlayer setVolume:0 fadeDuration:audioTrackDuration - fadeDuration];
Sink answered 28/2, 2018 at 15:17 Comment(0)
J
0

Swift solution:

The top rated answer here is great but it gives a stuttering effect as the volume step of 0.1 is too much. Using 0.01 gives a smoother fade effect to hear.

Using this code you can specify how long you want the fade transition to last.

let fadeVolumeStep: Float = 0.01

let fadeTime = 0.5 // Fade time in seconds

var fadeVolumeStepTime: Double {
     return fadeTime / Double(1.0 / fadeVolumeStep)
}

func fadeOut() {
    guard let player = self.player else {
        return
    }

    if !player.playing { return }

    func fadeOutPlayer() {
        if player.volume > fadeVolumeStep {
            player.volume -= fadeVolumeStep
            delay(time: fadeVolumeStepTime, closure: {
                fadeOutPlayer()
            })
        } else {
            player.stop()
            player.currentTime = 0
            player.prepareToPlay()
        }
    }

    fadeOutPlayer()
}

func fadeIn() {
    guard let player = self.player else {
        return
    }

    if player.playing { return }
    player.volume = 0
    player.play()

    func fadeInPlayer() {
        if player.volume <= 1 - fadeVolumeStep {
            player.volume += fadeVolumeStep
            delay(time: fadeVolumeStepTime, closure: {
                fadeInPlayer()
            })
        } else {
            player.volume = 1
        }
    }

    fadeInPlayer()
}

func delay(time delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

you can adjust the time using fadeTime constant.

Johst answered 22/8, 2016 at 8:42 Comment(1)
You can't, really, do this - use a display link. (it's actually much easier). do note that setVolume#fadeDuration exists now, so this is all academic.Calcimine
P
0

2024:

play and stop with FadeIn and FadeOut for AVAudioPlayer:

extension AVAudioPlayer {
    func playFadeIn(endVolume: Float, duration: Double) {
        self.volume = 0
        self.play()
        self.setVolume(endVolume, fadeDuration: duration)
    }
    
    func stopFadeOut(endVolume: Float = 0, duration: Double) {
        self.setVolume(endVolume, fadeDuration: duration)
        
        DispatchQueue.main.asyncAfter(delay: duration) {
            self.stop()
        }
    }
}
Piranesi answered 14/7, 2024 at 21:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.