简体   繁体   中英

Jquery - fade in audio after fading out

In my program I have background music on the start screen. This function is to fade my back ground sound out once the start button is clicked.

$(bgMusic).on('timeupdate', function () {
    var vol = 1,
        interval = 250;
    if (bgMusic.volume == 1) {
        var intervalID = setInterval(function () {
            if (vol > 0) {
                vol -= 0.05;
                bgMusic.volume = vol.toFixed(2);
            } else {
                clearInterval(intervalID);
            }
        }, interval);
    }
});

I have now added a restart button which takes you back to the start screen so I need to fade the music back in at the same speed. I have tried a few things but they don't work, can someone help me?

I need something along these lines:

$(bgMusic).off('timeupdate', function () {
    var vol = 0,
        interval = 250;
    if (bgMusic.volume == 0) {
        var intervalID = setInterval(function () {
            if (vol < 0) {
                vol += 0.05;
                bgMusic.volume = vol.toFixed(2);
            } else {
                clearInterval(intervalID);
            }
        }, interval);
    }
});

There are probably better ways to do this, but I just wrote a volume fader to get more accustomed to <audio> .

To enable it on your <audio> by enableFader(yourAudioNode) , then to fade out call yourAudioNode.fadeOut() , or to fade in call yourAudioNode.fadeIn() .
It is based around listening for timeupdate so unless you specify a slower rate, it will change the volume at the frequency explained here .

function enableFader(node, per_step, min_interval) {
    'use strict';
    var fadeOut, fadeIn;
    node.fadeSettings = {
        dir : 0,
        step : per_step || 0.05,
        interval : min_interval || 0.150,
        lastTime : -Infinity
    };
    fadeOut = function fadeOut() {
        var vol = this.volume,
            settings = this.fadeSettings,
            ctime = this.currentTime,
            dtime = Math.abs(ctime - settings.lastTime);
        if (settings.dir === -1 && dtime >= settings.interval && vol > 0) {
            settings.lastTime = ctime;
            this.volume = (vol - settings.step).toFixed(2);
        } else if (vol <= 0 || settings.dir !== -1) {
            this.removeEventListener('timeupdate', fadeOut);
            settings.dir = 0;
        }
    };
    fadeIn = function fadeIn() {
        var vol = this.volume,
            settings = this.fadeSettings,
            ctime = this.currentTime,
            dtime = Math.abs(ctime - settings.lastTime);
        if (settings.dir === 1 && dtime >= settings.interval && vol < 1) {
            settings.lastTime = ctime;
            this.volume = (vol + settings.step).toFixed(2);
        } else if (vol >= 1 || settings.dir !== 1) {
            this.removeEventListener('timeupdate', fadeIn);
            settings.dir = 0;
        }
    };
    node.fadeOut = function () {
        this.fadeSettings.dir = -1;
        this.addEventListener('timeupdate', fadeOut, false);
    };
    node.fadeIn = function () {
        this.fadeSettings.dir = 1;
        this.addEventListener('timeupdate', fadeIn, false);
    };
}

I tested it on this page using my dev console in Google Chrome.

var a = document.getElementById('song');
enableFader(a);
a.fadeOut();
// a.fadeIn();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM