It's pretty simple to get started really:
function playSound(url) {
var a = new Audio(url);
a.play();
}
Use that as whatever event handler you want for your application. Of course, I'd hope you'd want to do more than just play (for example, maybe pause would be good too?), but it's a start.
Answer from nickf on Stack OverflowIt's pretty simple to get started really:
function playSound(url) {
var a = new Audio(url);
a.play();
}
Use that as whatever event handler you want for your application. Of course, I'd hope you'd want to do more than just play (for example, maybe pause would be good too?), but it's a start.
let sound = new Audio("http://sound.com/mySound.mp3");
//on play event:
sound.onplay = () => {
};
//on pause event:
sound.onpause = () => {
};
//on end event:
sound.onended = () => {
};
//play:
sound.play();
//pause:
sound.pause();
//stop:
sound.pause();
sound.currentTime = 0;
Play sound on hyperlink? - JavaScript - SitePoint Forums | Web Development & Design Community
html - playing audio in JavaScript from URL with variable - Stack Overflow
javascript - How to play audio? - Stack Overflow
How to play an audio file from an external url using Javascript? - Stack Overflow
I believe you have to pass the extension here as well. This does work because this server is CORS enabled.
form.addEventListener("submit", myFunction)
function myFunction(e) {
e.preventDefault();
audio.src = 'https://bobzilla07.github.io/Music_Tap/' + myInput.value + '.mp3';
audio.play();
}
audio { display:none;}
<form id="form">
<input id="myInput" value="Panic-Song">
<input type="submit">
</form>
<audio controls id="audio"></audio>
Song list
You can do it like this:
let id = document.getElementById("myInput").value;
let audio = new Audio(`https://bobzilla07.github.io/Music_Tap/${id}.mp3`);
audio.play();
Now you can search for the file names of your songs. Like 1979 will play 1979.mp3.
If you don't want to mess with HTML elements:
var audio = new Audio('audio_file.mp3');
audio.play();
function play() {
var audio = new Audio('https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3');
audio.play();
}
<button onclick="play()">Play Audio</button>
This uses the HTMLAudioElement interface, which plays audio the same way as the <audio> element.
If you need more functionality, I used the howler.js library and found it simple and useful.
<script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.1.1/howler.min.js"></script>
<script>
var sound = new Howl({
src: ['https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3'],
volume: 0.5,
onend: function () {
alert('Finished!');
}
});
sound.play()
</script>
It's easy, just get your audio element and call the play() method:
document.getElementById('yourAudioTag').play();
Check out this example: http://www.storiesinflight.com/html5/audio.html
This site uncovers some of the other cool things you can do such as load(), pause(), and a few other properties of the audio element.
Audio works correctly cross server without an additional issues, see the example below.
There can be a number of reasons why this wouldn't work:
- Check the network tab to make sure the wav file is in the correct location.
- Check the console for any warning messages of why it couldn't load.
- Check this question/answer for additional debugging steps.
- Post back with the URL that you're trying to grab from, or additional information about the request.
async function playAudio() {
var audio = new Audio('https://www2.cs.uic.edu/~i101/SoundFiles/StarWars60.wav');
audio.type = 'audio/wav';
try {
await audio.play();
console.log('Playing...');
} catch (err) {
console.log('Failed to play...' + err);
}
}
<a href="#" onclick="playAudio()">Play Audio</a>
Try using the import function instead:
const playAudio = async () => {
const importRes = await import("./path/to/audio.mp3"); // make sure the path is correct
var audio = new Audio(importRes.default);
try {
await audio.play();
console.log("Playing audio");
} catch (err) {
console.log("Failed to play, error: " + err);
}
}
» npm install play-audio-url
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.