javascript - Stop playing audio when new audio is played -
if button pushed should play new sound , stop current sound playing. should make when sound playing class "pause" toggled on. if sound associated button not playing class normal "play".
//this buttons can pressed play sound <span class="play" sound="sound1.mp3">play</span> <span class="play" sound="sound2.mp3">play</span> <span class="play" sound="sound3.mp3">play</span> <script> // trying make var audio selects sound based on button pressed var audio = document.createelement('sound'); //when button pressed plays sound $(".play").on('click', function(){ if (audio.paused) { audio.play(); } else { audio.pause(); } $( ).toggleclass( "pause" ); // switch new css pause button }); // make if audio playing , sound pressed, audio playing stopped , newly pressed sound plays. if other audio playing{ audio.stop , this.audio. clicked. play } // make css of playing button switched , css of new button turned pause class. $( ).toggleclass( "play" ); $( ).toggleclass( "pause" ); </script>
i embed audio elements in spans this:
<span class="play"><audio src="sound1.mp3">play</audio></span> <span class="play"><audio src="sound2.mp3">play</audio></span> <span class="play"><audio src="sound3.mp3">play</audio></span>
and jquery magic
$(".play").click(function () { $(".play").removeclass('pause').addclass('play').pause(); $(this).addclass('pause').removeclass('play').play(); });
Comments
Post a Comment