javascript - Is it possible to make a jQuery click event triggering a function that plays a sound overlap? -
i trying make simple drum sampler using simple javascript , jquery. have jquery click events, along keyboard input events triggering function plays desired sound. function not trigger again while sound still playing.
example: hi-hat sound play again after entire wav file played.
i need sounds able overlap on create beat. possible jquery?
code example:
$(document).ready(function() { var hh = document.createelement('audio'); hh.setattribute('src', 'assets/sounds/hh.wav'); function rubytrigger() { hh.play(); $("#ruby-cover").animate({opacity: "0.5"}, 50); $("#ruby-cover").animate({opacity: "1"}, 75); } $("#ruby").click(function() { rubytrigger(); }); $(document).keypress(function(e) { if (e.which == 106) { rubytrigger(); } }); }
move document.createelement('audio')in rubytrigger function.
function rubytrigger() { var hh = document.createelement('audio'); hh.setattribute('src', 'http://www.soundjig.com/mp3/soundfx/drumkits/cy0010.mp3'); hh.play(); } $("#ruby").click(function() { rubytrigger(); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button id="ruby"> click </button> this create new audio element every time click button without effecting still playing.
i'm not sure assume these elements garbage collected when audio finishes playing.
Comments
Post a Comment