環境
chrome 44.0.2403.130 m (64-bit)
example
一定間隔でフェードアウト
volume = 1 - a*frame
music = new Audio(); music.src = "your/music/path.mp3"; music.fadeTimerId = null; music.fadeOut = function (time) { var frame = 30; var frameCount = 0; var endFrame = time * frame; var framePerSecond = 1/frame; var start = this.volume; var step = this.volume / (endFrame); var that = this; that.fadeTimerId = setInterval(function(){ var nextVolume = start - step*frameCount ; if (nextVolume <= 0) { clearInterval(that.fadeTimerId); that.pause(); that.volume = 1; return; } that.volume = nextVolume; ++frameCount; },framePerSecond * 1000); }
滑らかにフェードアウト
volume = 1 - a*frame2
music = new Audio(); music.src = "your/music/path.mp3"; music.fadeTimerId = null; music.fadeOut = function (time) { var frame = 30; var frameCount = 0; var endFrame = time * frame; var framePerSecond = 1/frame; var start = this.volume; var step = this.volume / (endFrame*endFrame); var that = this; that.fadeTimerId = setInterval(function(){ var nextVolume = start - step*frameCount*frameCount ; if (nextVolume <= 0) { clearInterval(that.fadeTimerId); that.pause(); that.volume = 1; return; } that.volume = nextVolume; ++frameCount; },framePerSecond * 1000); }