javascript - How to add a mute button to my app? -
i have few events in app make sounds.
for example,
- background music looped
- background on click makes sound
- when 2 rectangles collide makes sound
what want have button when clicked toggles between letting sounds play , not letting them play.
i don't want have load of if statements on each sound, there way around ?
here how im calling sounds @ moment
//html <div id='mainright'></div> //js var mainright = $('#mainright'); $(mainright).width(windowdim.width/2).height(windowdim.height); $(mainright).addclass('mainright'); var sounds = { coinhit : new audio('./sound/coincollect.wav'), playerclick : new audio('./sound/playerclick.wav'), gameover : new audio('./sound/gameover.wav'), backgroundmusic : new audio('./sound/backgroundmusic.wav') } sounds.backgroundmusic.addeventlistener('ended', function() { this.currenttime = 0; this.play(); }, false); sounds.backgroundmusic.play(); sounds.backgroundmusic.volume = 0.01; $('#mainright').click(function() { sounds.playerclick.load(); sounds.playerclick.play(); }
try this...
function toggleaudio() { for(var key in sounds) { sounds[key].muted = !sounds[key].muted; } }
just fire every time hit mute button. toggle muted state of each audio
object in sounds
object.
Comments
Post a Comment