Skip to content

Instantly share code, notes, and snippets.

@loudnarrative
Created April 16, 2012 03:18
Show Gist options
  • Save loudnarrative/2396206 to your computer and use it in GitHub Desktop.
Save loudnarrative/2396206 to your computer and use it in GitHub Desktop.
Fade out HTML5 audio
// every second your audio element is playing
$(audioElement).on('timeupdate', function() {
var vol = 1,
interval = 200; // 200ms interval
if (Math.floor(audioElement.currentTime) == 15) {
if (audioElement.volume == 1) {
var intervalID = setInterval(function() {
// Reduce volume by 0.05 as long as it is above 0
// This works as long as you start with a multiple of 0.05!
if (vol > 0) {
vol -= 0.05;
// limit to 2 decimal places
// also converts to string, works ok
audioElement.volume = vol.toFixed(2);
} else {
// Stop the setInterval when 0 is reached
clearInterval(intervalID);
}
}, interval);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment