I'm attempting to create a link that plays sound when it is pressed. I found a solution describing how to do this, but when I implemented it, I wasn't able to produce sound. The relevant HTML is below.
<div class="choices">
<a class="buttons" id="choice1" href="#" onclick="choiceSelected();return false;"></a>
<a class="buttons" id="choice2" href="#" onclick="choiceSelected();return false;"></a>
<a class="buttons" id="choice3" href="#" onclick="choiceSelected();return false;"></a>
<a class="buttons" id="choice4" href="#" onclick="choiceSelected();return false;"></a>
</div>
And here is the javascript (it is in an external file that is linked in the head of my document):
function choiceSelected(){
var audio = new Audio("buttonClick.mp3");
audio.play();
console.log("Audio should have played");
}
I added the console.log("Audio should have played");
to test if the function was working and it is. The console displays "Audio should have played" and reports no errors. Everything seems to be working, except there is no sound produced. The sound file buttonClick.mp3
is in the correct path.
I have tried using different file types (OGG, AAC, etc.) and none of them have worked. I have also attempted replacing the file with a link to an online file that I know works, so I don't think it is an issue with the file.
I saw there were some problems with Safari not playing audio to preserve power, but those articles looked to be at least 2-3 years old, so I am not sure that is the issue either (if it still is an issue, could you please let me know).
Any assistance with getting the audio to work would be greatly appreciated.