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.
Play sound on click, then go to page
How to play an audio when clicking on a link, or just clicking in general?
playing sound on click event with pure javascript - Stack Overflow
javascript - Playing sound on a button click - Code Review Stack Exchange
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.
There is no need to include the audio tags. Instead you can just say in javascript what audio you want to play.
function myPlay(){
var audio = new Audio("sample.mp3");
audio.play();
}
When you execute this function the audio will play.
Try this example, https://jsfiddle.net/nerdvoso/46f7rxbs/1/
Following code:
var playBtn = document.getElementById('play');
var stopBtn = document.getElementById('stop');
var playSound = function() {
audio.play();
};
playBtn.addEventListener('click', playSound, false);
stopBtn.addEventListener('click', function(){audio.pause()}, false);
<audio id="audio" src="https://freewavesamples.com/files/Roland-JV-2080-101-Bass-C2.wav" preload="auto"></audio>
<button id="play">Play</button>
<button id="stop">Stop</button>
Update, https://jsfiddle.net/nerdvoso/46f7rxbs/33/
var playBtn = document.getElementById('play');
var stopBtn = document.getElementById('stop');
var nextBtn = document.getElementById('next');
var prevBtn = document.getElementById('prev');
var soundSelected = document.getElementById("audio1");
var playSound = function() {soundSelected.play();};
var stopSound = function() {soundSelected.pause();};
var nextSound = function() {
if(soundSelected.nextElementSibling){
soundSelected.pause();
soundSelected.currentTime = 0;
soundSelected = soundSelected.nextElementSibling;
}
};
var prevSound = function() {
if(soundSelected.previousElementSibling){
soundSelected.pause();
soundSelected.currentTime = 0;
soundSelected = soundSelected.previousElementSibling;
}
};
playBtn.addEventListener('click', playSound, false);
stopBtn.addEventListener('click', stopSound, false);
nextBtn.addEventListener('click', nextSound, false);
prevBtn.addEventListener('click', prevSound, false);
<div id="playList">
<audio id="audio1" src="https://freewavesamples.com/files/Roland-JV-2080-101-Bass-C2.wav" preload="auto"></audio>
<audio id="audio2" src="https://freewavesamples.com/files/Yamaha-V50-Metalimba-C2.wav" preload="auto"></audio>
</div>
<button id="prev">Prev</button>
<button id="play">Play</button>
<button id="stop">Stop</button>
<button id="next">Next</button>
Play sound by dedicated buttons, enter link description here
var playSoundBtn = document.getElementsByClassName("playSound");
var playSound = function() {
var attribute = this.getAttribute("data-sound");
var sounds = document.getElementsByTagName('audio');
for(i=0; i<sounds.length; i++){
sounds[i].pause();
sounds[i].currentTime = 0;
}
document.getElementById(attribute).play();
};
for (var i = 0; i < playSoundBtn.length; i++) {
playSoundBtn[i].addEventListener('click', playSound, false);
}
<audio id="audio1" src="https://freewavesamples.com/files/Roland-JV-2080-101-Bass-C2.wav" preload="auto"></audio>
<audio id="audio2" src="https://freewavesamples.com/files/Yamaha-V50-Metalimba-C2.wav" preload="auto"></audio>
<hr>
<button class="playSound" data-sound="audio1">Sound 1</button>
<button class="playSound" data-sound="audio2">Sound 2</button>
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" />
Use below code, if the melodies are at some other location. In Javascript, provide below:
<script>
function PlaySound(melody) {
alert("On Press of "+melody);
var path = "path\\to\\melody\\"
var snd = new Audio(path + melody + ".mp3");
snd.play();
}
</script>
In HTML body: you can insert
<button onclick="PlaySound('melody1')"> button1</button>
<button onclick="PlaySound('melody2')"> button2</button>
Keep it simple (until it works) and try this:
<audio id="bflat" src="bflat.mp3"></audio>
<button onclick="document.getElementById('bflat').play()">Play!</button>
Found it at MDN