2

I have a problem with getting sound to play and pause within my JS and HTML code.

I have a function that takes the two tracks inputted, pauses one, and plays another, for use to create dynamic music based on where the user is or what it is doing. However when the function is called, neither track is played or paused. I've looked up online to make sure that I'm using the proper audio formatting and from my research I have been. I'm currently stumped as to why this isn't working so a fresh set of eyes may help to figure out the problem.

My Javascript:

function playTrack(musicon, musicoff) {
document.getElementById(musicon).play();
document.getElementById(musicoff).pause();
}

My HTML

   <audio id="themeD" autoplay="autoplay" loop="true">
    <source src="d.ogg" type="audio/ogg" />
    <source src="d.mp3" type="audio/mpeg" />
   </audio>

   <audio id="themeE" loop="true">
    <source src="e.ogg" type="audio/ogg" />
    <source src="e.mp3" type="audio/mpeg" />
   </audio>


   <a class="button" onclick="javascript:playTrack('ThemeE','ThemeD');">Change audio</a>

Somewhat of a beginner with Javascript and definitely a noob with audio elements so any and all help or tips would be appreciated.

1 Answer 1

2

Watch your spaces. You play the audio with the id Theme E, but the id of your audio is themeE

3
  • Just needed a fresh new set of eyes to look it over haha. I fixed that part, but the audio still won't play.
    – Timble
    Feb 5, 2016 at 7:01
  • Don't forget to check your caps. ThemeE as apposed to `themeE'
    – Josh
    Feb 5, 2016 at 7:05
  • ah ha, thank you that fixed it. I feel silly for not noticing those things lol.
    – Timble
    Feb 5, 2016 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.