Cannot get audio to play in JavaScript soundboard (total newbie here)
Cannot get audio to play in JavaScript soundboard (total newbie here)
I'm trying to create, as a starting excercise, a soundboard for our D&D sessions. I'm currently trying to make it so that when a button is clicked, the variable 'audio' is set to that, and that it then runs the function to play or pause audio, depending on whether it's running or not. I have JS and HTML in the same file (I realise this isn't good, however). Below is my code:
<button id="play1st">Heroic Battle Music</button>
<button id="play2nd">Military Battle Music</button>
<button id="play3rd">Stronghold Battle Music</button>
<!--Audio to be played -->
<audio id="heroic" src="heroic.mp3"></audio>
<audio id="combat_rome" src="combat_rome.mp3"></audio>
<audio id="stronghold" src="stronghold.mp3"></audio>
<!--JavaScript audio -->
<script>
function playMusic()
document.getElementById("play1st").addEventListener ('click', music1());
document.getElementById("play2nd").addEventListener ('click', music2());
document.getElementById("play3rd").addEventListener ('click', music3());
var isPlaying = false;
function music1()
var audio = new Audio("heroic.mp3");
togglePlay();
;
function music2()
var audio = new Audio("combat_rome.mp3");
togglePlay();
;
function music3()
var audio = new Audio("stronghold.mp3");
togglePlay();
;
function togglePlay()
if (isPlaying)
pause()
else
audio.play();
;
audio.onplaying = function ()
isPlaying = true;
;
audio.onpause = function ()
isPlaying = false;
;
</script>
I'm probably missing something very basic, so any explanation would be most welcome. Thank you!
Is it just me missing it, or is the function
playMusic()
never called? I guess, that the onclick
-events won't fire, since they get defined in the playMusic()
-function, which never gets called. Just for fun, try removing the outer shell function playMusic() [...] ;
– user10004359
Sep 6 '18 at 11:08
playMusic()
onclick
playMusic()
function playMusic() [...] ;
I see numerous problems. Suggest you start by getting one sound file able to start/stop before compounding the same problems into multiple functions
– charlietfl
Sep 6 '18 at 11:13
@D.Schaller Correct, not a single function is being called in the HTML. (or event)
– Sparky
Sep 6 '18 at 11:13
@D.Schaller Thank you for your comment! I tried removing it, but then get the following error message in chrome:Uncaught ReferenceError: audio is not defined at togglePlay (index.html:54) at music1 (index.html:39) at index.html:33
– Yoram
Sep 6 '18 at 11:28
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Welcome to StackOverflow. Do you get any error message? If so please add it to your question.
– Jakob Sachs
Sep 6 '18 at 11:04