Toby Forster
Toby Forster

Reputation: 1

I am trying to play an mp3 file when I press a button on html

...and it is not working. I have looked through the various other questions on this website which ask the same thing. None of them seem to fix the problem and often create new problems. I am fairly new to HTML/JS, so the solution could be incredibly simple.

<script language="javascript">


        function No(){

            var audioNo = new Audio();
            No.src = "No.mp3";
            document.getElementById(No);
            audioNo.Play();

        }

    </script>

</head>

<body>

    <p id="No" onclick="No()" type="text"> No </p>

</body>

Upvotes: 0

Views: 61

Answers (2)

haxxxton
haxxxton

Reputation: 6442

This looks like you've just made a linkage error in your js, try

<script>
        function No(){

            var audioNo = new Audio();
            audioNo.src = "No.mp3";
            audioNo.play();

        }
</script>
</head>
<body>

    <p id="No" onclick="No()" type="text"> No </p>

</body>

alternatively, have a look at something like this answer https://stackoverflow.com/a/18628124/648350 which places the src inside the Audio tag, and also suggests looking at howler.js if you require greater functionality

Upvotes: 1

M14
M14

Reputation: 1810

I created a jsfiddle

https://jsfiddle.net/stjqrry4/

<p id="No" onclick="play()" type="text"> No </p>

<script>
        function play(){
            var audioNo = new Audio();
            audioNo.src = "http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3";
            audioNo.play();

        }

    </script>

Upvotes: 1

Related Questions