Try the below code snippet
<!doctype html>
<html>
<head>
<title>Audio</title>
</head>
<body>
<script>
function play() {
var audio = document.getElementById("audio");
audio.play();
}
</script>
<input type="button" value="PLAY" onclick="play()">
<audio id="audio" src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"></audio>
</body>
</html>
Answer from Arunkumar Vasudevan on Stack OverflowTry the below code snippet
<!doctype html>
<html>
<head>
<title>Audio</title>
</head>
<body>
<script>
function play() {
var audio = document.getElementById("audio");
audio.play();
}
</script>
<input type="button" value="PLAY" onclick="play()">
<audio id="audio" src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"></audio>
</body>
</html>
JavaScript
function playAudio(url) {
new Audio(url).play();
}
HTML
<img src="image.png" onclick="playAudio('mysound.mp3')">
Supported in most modern browsers and easy to embed into HTML elements.
If you only need to support recent browsers, then HTML 5 offers you the Audio object
to load/buffer your sound:
var snd = new Audio("file.wav");
to play the sound:
snd.play();
to re-cue it to the beginning (so that you can play it again):
snd.currentTime=0;
This answer https://stackoverflow.com/a/7620930/1459653 by @klaustopher (https://stackoverflow.com/users/767272/klaustopher) helped me. He wrote:
HTML5 has the new
<audio>-Tag that can be used to play sound. It even has a pretty simple JavaScript Interface:
<audio id="sound1" src="yoursound.mp3" preload="auto"></audio> <button onclick="document.getElementById('sound1').play();">Play it</button>
Here's how I implemented his advice so that clicking on the Font Awesome icon "fa-volume-up" (located on the Web page after "mule.") results in "donkey2.mp3" sound playing (note: mp3 doesn't play in all browsers).
<p>In short, you're treated like a whole person, instead of a rented mule. <audio id="sound1" src="assets/donkey2.mp3" preload="auto"></audio><a class="icon fa-volume-up" onclick="document.getElementById('sound1').play();"></a>
Play sound on click, then go to page
html - How can I play a random sound on click in a web page? - Stack Overflow
Playing sound on a button click
How to play an audio when clicking on a link, or just clicking in general?
Videos
I apologize for the lack of any code here, I am extremely new to HTML and am trying to create a personal website. I want to play a silly sound effect for when you click on anything on this page, but i can't seem to find suitable code, or just code that makes any sense to me.
I want the audio to be able to replay, not take you to the source of the audio, y know, just a simple sound effect.
Use JavaScript to Add Sound
Place the following script in the <head> of your HTML document:
<script language="javascript" type="text/javascript">
function playSound(soundfile) {
document.getElementById("dummy").innerHTML=
"<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}
</script>
The Sound is Placed in an Empty Span
The JavaScript places an embed tag inside an empty span tag when the script is initiated. So, you need to add the following span file somewhere within the body of your HTML page, preferabl near the top of the document:
<span id="dummy"></span>
Call the Script with an onmouseover or onclick Attribute
The last thing you need to add is an element that you want to generate the sound on click or on mouseover. Call the script with one of those attributes:
<a href="#" onclick="playSound('URL to soundfile');">Click here to hear a sound</a>
<p onmouseover="playSound('URL to soundfile');">Mouse over this text to hear a sound</p>
If you have the ID of your audio element, you can do this :
document.getElementById(theId).play();
The audio element could look like this :
<audio id="someId">
<source src=sound/zbluejay.wav>
</audio>
And if you need it, you may add the audio element dynamically like this :
document.write("<audio id=someId><source src=yourURL</audio>");
document.getElementById('someId').play();
Fiddle here.
Don't duplicate sections of code! In particular, avoid putting 'execution' code in these simple conditionals, instead use them to build a shared executor:
function playSound(soundfile) {
var type_attr = '';
if (navigator.userAgent.indexOf('Firefox') != -1) {
type_attr = "type=\"application/x-mplayer2\"";
}
document.getElementById("dummy").innerHTML = "<embed src=\"" + soundfile + "\" hidden=\"true\" autostart=\"true\" loop=\"false\" " + type_attr + " />";
}
Regarding the HTML:
script element
For JavaScript, you can simply use <script> without the type attribute:
If the language is not that described by "text/javascript", then the
typeattribute must be present
The language attribute is obsolete:
Authors should not specify a
languageattribute on ascriptelement.
a element
I'd say a real button should be used here, not a link:
buttonelement withbuttontype:<button type="button" onclick="…">Click here to hear a sound</button>inputelement withbuttontype:<input type="button" onclick="…" value="Click here to hear a sound" />