Created
May 25, 2012 13:56
-
-
Save jaymzcd/2788289 to your computer and use it in GitHub Desktop.
Quick'n'Easy HTML5 volume fader on audio tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var CONFIG = { | |
| snd: '#sound', // jquery selector for element | |
| fadeout_time: 100, // miliseconds for volume change | |
| fadeout_step: 0.1, // amount to reduce by | |
| max_volume: 1 // change to limit maximum | |
| } | |
| function volumeFader(end_volume, step, time, callback) { | |
| var current_volume = $(CONFIG.snd)[0].volume; | |
| if(end_volume<current_volume) { | |
| step = -1*step; | |
| } | |
| if(DEBUG) console.log("Fading sound", current_volume, end_volume, step, time); | |
| var volumeChange = function () { | |
| if(DEBUG) console.log("Reducing sound", current_volume); | |
| var delta = Math.abs(current_volume-end_volume); | |
| if((delta>0) && (delta>Math.abs(step))) { | |
| current_volume += step; | |
| $(CONFIG.snd)[0].volume = current_volume; | |
| } else { | |
| $(CONFIG.snd)[0].volume = end_volume; | |
| clearInterval(volTimer); | |
| if(callback!==undefined) { | |
| callback(); | |
| } | |
| } | |
| }; | |
| var volTimer = setInterval(volumeChange, time); | |
| } | |
| // Usage: | |
| volumeFader(CONFIG.max_volume, CONFIG.fadeout_step, CONFIG.fadeout_time); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment