javascript - turn off audio progressively on click -


i have engine song in autoplay , want when click on button sound turn off "progressively" fluid diminution of volume , finish @ 0.1 or 0.2 of volume, have idea ? actual code:

<audio autoplay>    <source src="../audio/2445.mp3"> </audio>  <button>turn off</button> 

not sure if right way it, like:

<audio autoplay id='aud'>    <source src='http://upload.wikimedia.org/wikipedia/commons/5/5b/ludwig_van_beethoven_-_symphonie_5_c-moll_-_1._allegro_con_brio.ogg'> </audio>  <button id='btn'>turn off</button> 

js:

var audio = document.getelementbyid('aud'), interval; document.getelementbyid('btn').onclick = turnoff; function turnoff(){     if(audio && !audio.paused){         interval = setinterval(function(){             console.log('audio volume: ', audio.volume);             if(!audio || audio.paused || audio.volume < 0.1){                 clearinterval(interval);             }else{                 audio.volume -= .05; // change value per liking             }         }, 200); // change value per liking     } } 

i guess better way might web-audio.

fiddle demo


Comments