javascript - Play and pause to play sound -


im trying have these 4 buttons, added in code impliment 5th 1 later, can ignore 1 atm. want have when click on 1 button plays sound, if click on different one, pauses current sound , plays new one. dont seem working.

code:

<audio id="sound1" src="monkey.mp3"></audio> <audio id="sound2" src="sounds/sound2.mp3"></audio> <audio id="sound3" src=".mp3"></audio> <audio id="sound4" src=".mp3"></audio> <audio id="sound5" src=".mp3"></audio>  <button onclick="playsound1()">sound 1</button><br /> <button onclick="playsound2()">sound 2</button><br /> <button onclick="playsound3()">sound 3</button><br /> <button onclick="playsound4()">sound 4</button><br /> <button onclick="playsound5()">sound 5</button><br />  <script type="text/javascript"> var audio1 = document.getelementbyid('sound1'); var audio2 = document.getelementbyid('sound2'); var audio3 = document.getelementbyid('sound3'); var audio4 = document.getelementbyid('sound4'); function playsound1(){ if ((audio1.paused !== true) && (audio.3paused !== true) && (audio4.paused !==){     audio1.pause();     audio3.pause();     audio4.pause();     audio2.play();     } else{     audio2.play();     } } function playsound2(){ if (audio2.paused !== true){     audio2.pause();     audio3.pause();     audio4.pause();     audio1.play();     } else{     audio1.play();     } } function playsound3(){ if (audio2.paused !== true){     audio2.pause();     audio1.pause();     audio3.pause();     audio4.play();     } else{     audio4.play();     } } function playsound4(){ if (audio2.paused !== true){     audio2.pause();     audio4.pause();     audio1.pause();     audio3.play();     } else{     audio3.play();     } }  </script> 

var audio1 = document.getelementbyid('sound1');  var audio2 = document.getelementbyid('sound2');  var audio3 = document.getelementbyid('sound3');  var audio4 = document.getelementbyid('sound4');    var current = "";    var playsound = function(soundnumber){      switch(soundnumber)        {          case 1:            audio1.play();            audio2.pause();            audio3.pause();            audio4.pause();            break;          case 2:            audio1.pause();            audio2.play();            audio3.pause();            audio4.pause();            break;          case 3:            audio1.pause();            audio2.pause();            audio3.play();            audio4.pause();            break;          case 4:            audio1.pause();            audio2.pause();            audio3.pause();            audio4.play();            break;          default:            var audio = document.getelementbyid("sound"+soundnumber);            audio.play();            audio1.pause();            audio2.pause();            audio3.pause();            audio4.pause();            break;        }  }
<audio id="sound1" src="monkey.mp3"></audio>  <audio id="sound2" src="sounds/sound2.mp3"></audio>  <audio id="sound3" src=".mp3"></audio>  <audio id="sound4" src=".mp3"></audio>  <audio id="sound5" src=".mp3"></audio>    <button onclick="playsound(1)">sound 1</button><br />  <button onclick="playsound(2)">sound 2</button><br />  <button onclick="playsound(3)">sound 3</button><br />  <button onclick="playsound(4)">sound 4</button><br />  <button onclick="playsound(5)">sound 5</button><br />

the button °5 should work default switch case. if you'll have add buttons generically, function have changed.


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -