Audio will not play - JS -












0















Struggled with this for hours - Can't seem to get the audio file to play once I click an HTML element.






var bb = document.querySelector(".soundfile");

function playAudio() {
bb.play();
}


document.querySelector(".btn-hold").addEventListener("click", playAudio);

<audio class="soundfile">
<sound src="sound.mp3" type="audio/mpeg">
</audio>

<button class="btn-new"><i class="ion-ios-plus-outline"></i>New game</button>
<button class="btn-roll"><i class="ion-ios-loop"></i>Roll dice</button>
<button class="btn-hold"><i class="ion-ios-download-outline"></i>Hold</button>





this is different to other questions










share|improve this question

























  • Possible duplicate of Javascript Audio Play on click

    – SilvioCro
    Nov 22 '18 at 1:18











  • I'm a little confused by your code. If you're asking about playing a sound file from a click, why are you showing buttons labeled "new game", "roll dice" and "hold"? Can you create a plain Minimal, Complete, and Verifiable example instead?

    – Mike 'Pomax' Kamermans
    Nov 22 '18 at 1:39






  • 3





    it should be <source> not <sound>. No?

    – Yong Quan
    Nov 22 '18 at 1:39











  • @Mike'Pomax'Kamermans I imagine OP wants to play a sound when you click the "Hold" button. There's enough information here to understand the problem (which YongQuan has solved above)

    – Phil
    Nov 22 '18 at 1:40













  • As do I, and the Minimal, Complete, and Verifiable example request is not just for us, but also for James himself: 99.9% of the time simply the exercise of creating that minimal example makes you discover the actual problem all on your own, even if you already had your SO question written up.

    – Mike 'Pomax' Kamermans
    Nov 22 '18 at 1:41


















0















Struggled with this for hours - Can't seem to get the audio file to play once I click an HTML element.






var bb = document.querySelector(".soundfile");

function playAudio() {
bb.play();
}


document.querySelector(".btn-hold").addEventListener("click", playAudio);

<audio class="soundfile">
<sound src="sound.mp3" type="audio/mpeg">
</audio>

<button class="btn-new"><i class="ion-ios-plus-outline"></i>New game</button>
<button class="btn-roll"><i class="ion-ios-loop"></i>Roll dice</button>
<button class="btn-hold"><i class="ion-ios-download-outline"></i>Hold</button>





this is different to other questions










share|improve this question

























  • Possible duplicate of Javascript Audio Play on click

    – SilvioCro
    Nov 22 '18 at 1:18











  • I'm a little confused by your code. If you're asking about playing a sound file from a click, why are you showing buttons labeled "new game", "roll dice" and "hold"? Can you create a plain Minimal, Complete, and Verifiable example instead?

    – Mike 'Pomax' Kamermans
    Nov 22 '18 at 1:39






  • 3





    it should be <source> not <sound>. No?

    – Yong Quan
    Nov 22 '18 at 1:39











  • @Mike'Pomax'Kamermans I imagine OP wants to play a sound when you click the "Hold" button. There's enough information here to understand the problem (which YongQuan has solved above)

    – Phil
    Nov 22 '18 at 1:40













  • As do I, and the Minimal, Complete, and Verifiable example request is not just for us, but also for James himself: 99.9% of the time simply the exercise of creating that minimal example makes you discover the actual problem all on your own, even if you already had your SO question written up.

    – Mike 'Pomax' Kamermans
    Nov 22 '18 at 1:41
















0












0








0








Struggled with this for hours - Can't seem to get the audio file to play once I click an HTML element.






var bb = document.querySelector(".soundfile");

function playAudio() {
bb.play();
}


document.querySelector(".btn-hold").addEventListener("click", playAudio);

<audio class="soundfile">
<sound src="sound.mp3" type="audio/mpeg">
</audio>

<button class="btn-new"><i class="ion-ios-plus-outline"></i>New game</button>
<button class="btn-roll"><i class="ion-ios-loop"></i>Roll dice</button>
<button class="btn-hold"><i class="ion-ios-download-outline"></i>Hold</button>





this is different to other questions










share|improve this question
















Struggled with this for hours - Can't seem to get the audio file to play once I click an HTML element.






var bb = document.querySelector(".soundfile");

function playAudio() {
bb.play();
}


document.querySelector(".btn-hold").addEventListener("click", playAudio);

<audio class="soundfile">
<sound src="sound.mp3" type="audio/mpeg">
</audio>

<button class="btn-new"><i class="ion-ios-plus-outline"></i>New game</button>
<button class="btn-roll"><i class="ion-ios-loop"></i>Roll dice</button>
<button class="btn-hold"><i class="ion-ios-download-outline"></i>Hold</button>





this is different to other questions






var bb = document.querySelector(".soundfile");

function playAudio() {
bb.play();
}


document.querySelector(".btn-hold").addEventListener("click", playAudio);

<audio class="soundfile">
<sound src="sound.mp3" type="audio/mpeg">
</audio>

<button class="btn-new"><i class="ion-ios-plus-outline"></i>New game</button>
<button class="btn-roll"><i class="ion-ios-loop"></i>Roll dice</button>
<button class="btn-hold"><i class="ion-ios-download-outline"></i>Hold</button>





var bb = document.querySelector(".soundfile");

function playAudio() {
bb.play();
}


document.querySelector(".btn-hold").addEventListener("click", playAudio);

<audio class="soundfile">
<sound src="sound.mp3" type="audio/mpeg">
</audio>

<button class="btn-new"><i class="ion-ios-plus-outline"></i>New game</button>
<button class="btn-roll"><i class="ion-ios-loop"></i>Roll dice</button>
<button class="btn-hold"><i class="ion-ios-download-outline"></i>Hold</button>






javascript html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 1:35









Phil

96.3k11136157




96.3k11136157










asked Nov 22 '18 at 1:15









James HendersJames Henders

214




214













  • Possible duplicate of Javascript Audio Play on click

    – SilvioCro
    Nov 22 '18 at 1:18











  • I'm a little confused by your code. If you're asking about playing a sound file from a click, why are you showing buttons labeled "new game", "roll dice" and "hold"? Can you create a plain Minimal, Complete, and Verifiable example instead?

    – Mike 'Pomax' Kamermans
    Nov 22 '18 at 1:39






  • 3





    it should be <source> not <sound>. No?

    – Yong Quan
    Nov 22 '18 at 1:39











  • @Mike'Pomax'Kamermans I imagine OP wants to play a sound when you click the "Hold" button. There's enough information here to understand the problem (which YongQuan has solved above)

    – Phil
    Nov 22 '18 at 1:40













  • As do I, and the Minimal, Complete, and Verifiable example request is not just for us, but also for James himself: 99.9% of the time simply the exercise of creating that minimal example makes you discover the actual problem all on your own, even if you already had your SO question written up.

    – Mike 'Pomax' Kamermans
    Nov 22 '18 at 1:41





















  • Possible duplicate of Javascript Audio Play on click

    – SilvioCro
    Nov 22 '18 at 1:18











  • I'm a little confused by your code. If you're asking about playing a sound file from a click, why are you showing buttons labeled "new game", "roll dice" and "hold"? Can you create a plain Minimal, Complete, and Verifiable example instead?

    – Mike 'Pomax' Kamermans
    Nov 22 '18 at 1:39






  • 3





    it should be <source> not <sound>. No?

    – Yong Quan
    Nov 22 '18 at 1:39











  • @Mike'Pomax'Kamermans I imagine OP wants to play a sound when you click the "Hold" button. There's enough information here to understand the problem (which YongQuan has solved above)

    – Phil
    Nov 22 '18 at 1:40













  • As do I, and the Minimal, Complete, and Verifiable example request is not just for us, but also for James himself: 99.9% of the time simply the exercise of creating that minimal example makes you discover the actual problem all on your own, even if you already had your SO question written up.

    – Mike 'Pomax' Kamermans
    Nov 22 '18 at 1:41



















Possible duplicate of Javascript Audio Play on click

– SilvioCro
Nov 22 '18 at 1:18





Possible duplicate of Javascript Audio Play on click

– SilvioCro
Nov 22 '18 at 1:18













I'm a little confused by your code. If you're asking about playing a sound file from a click, why are you showing buttons labeled "new game", "roll dice" and "hold"? Can you create a plain Minimal, Complete, and Verifiable example instead?

– Mike 'Pomax' Kamermans
Nov 22 '18 at 1:39





I'm a little confused by your code. If you're asking about playing a sound file from a click, why are you showing buttons labeled "new game", "roll dice" and "hold"? Can you create a plain Minimal, Complete, and Verifiable example instead?

– Mike 'Pomax' Kamermans
Nov 22 '18 at 1:39




3




3





it should be <source> not <sound>. No?

– Yong Quan
Nov 22 '18 at 1:39





it should be <source> not <sound>. No?

– Yong Quan
Nov 22 '18 at 1:39













@Mike'Pomax'Kamermans I imagine OP wants to play a sound when you click the "Hold" button. There's enough information here to understand the problem (which YongQuan has solved above)

– Phil
Nov 22 '18 at 1:40







@Mike'Pomax'Kamermans I imagine OP wants to play a sound when you click the "Hold" button. There's enough information here to understand the problem (which YongQuan has solved above)

– Phil
Nov 22 '18 at 1:40















As do I, and the Minimal, Complete, and Verifiable example request is not just for us, but also for James himself: 99.9% of the time simply the exercise of creating that minimal example makes you discover the actual problem all on your own, even if you already had your SO question written up.

– Mike 'Pomax' Kamermans
Nov 22 '18 at 1:41







As do I, and the Minimal, Complete, and Verifiable example request is not just for us, but also for James himself: 99.9% of the time simply the exercise of creating that minimal example makes you discover the actual problem all on your own, even if you already had your SO question written up.

– Mike 'Pomax' Kamermans
Nov 22 '18 at 1:41














2 Answers
2






active

oldest

votes


















3














It should be <source>, not <sound>.



<audio class="soundfile">
<source src="sound.mp3" type="audio/mpeg">
</audio>


It should be working after you change to the correct element.



If it's still not working, then there might be a problem in some other part of your code and we have not enough information to help you diagnose that.






share|improve this answer

































    0














    Replace <sound> tag with <source>






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422625%2faudio-will-not-play-js%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      It should be <source>, not <sound>.



      <audio class="soundfile">
      <source src="sound.mp3" type="audio/mpeg">
      </audio>


      It should be working after you change to the correct element.



      If it's still not working, then there might be a problem in some other part of your code and we have not enough information to help you diagnose that.






      share|improve this answer






























        3














        It should be <source>, not <sound>.



        <audio class="soundfile">
        <source src="sound.mp3" type="audio/mpeg">
        </audio>


        It should be working after you change to the correct element.



        If it's still not working, then there might be a problem in some other part of your code and we have not enough information to help you diagnose that.






        share|improve this answer




























          3












          3








          3







          It should be <source>, not <sound>.



          <audio class="soundfile">
          <source src="sound.mp3" type="audio/mpeg">
          </audio>


          It should be working after you change to the correct element.



          If it's still not working, then there might be a problem in some other part of your code and we have not enough information to help you diagnose that.






          share|improve this answer















          It should be <source>, not <sound>.



          <audio class="soundfile">
          <source src="sound.mp3" type="audio/mpeg">
          </audio>


          It should be working after you change to the correct element.



          If it's still not working, then there might be a problem in some other part of your code and we have not enough information to help you diagnose that.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 6:03

























          answered Nov 22 '18 at 1:42









          Yong QuanYong Quan

          1,9341518




          1,9341518

























              0














              Replace <sound> tag with <source>






              share|improve this answer




























                0














                Replace <sound> tag with <source>






                share|improve this answer


























                  0












                  0








                  0







                  Replace <sound> tag with <source>






                  share|improve this answer













                  Replace <sound> tag with <source>







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 7:03









                  Miruku ShekiMiruku Sheki

                  15113




                  15113






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422625%2faudio-will-not-play-js%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python