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;
How to play an audio file from an external url using Javascript? - Stack Overflow
Play sound on hyperlink? - JavaScript - SitePoint Forums | Web Development & Design Community
How to play MP3 audio with dynamic URL in HTML5 or JavaScript? - Stack Overflow
javascript - How to play audio? - Stack Overflow
Videos
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.
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);
}
}
That's audio/mpeg, not audio/ogg, as seen from headers:
Content-Type:audio/mpeg
Try this:
<audio controls="controls">
<source src="http://translate.google.com/translate_tts?tl=en&q=Hello%2C+World" type="audio/mpeg">
</audio>
http://jsfiddle.net/ZCwHH/
Works in browsers that play mp3, like google chrome.
I am not sure what you are trying to achieve in your example but as far as playing audio with HTML5 is concerned you can simply do it in these ways:
Static Url:
<audio controls="controls">
<source src="mySong.mp3" type="audio/mpeg">
</audio>
Dynamic Url: (just print that url in the src attribute)
<audio controls="controls">
<source src="<%Reponse.Write(url);%>" type="audio/mpeg">
</audio>
Hope it solves this problem.
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.
Hello,
Im writing a wordpress plugin that monitors woocommerces shop_orders page. When a new order comes in the page is supposed to play a tada sound.
Everything is hooked into the shop_orders page in the admin. The idea is the user navigates to that page, every 30 seconds the page refreshes, during the refresh a filter is triggered and we check for the presence of a new order. This all works perfectly.
Now, when the new order is found, during this load we also output some javascript to play the notification sound:
if (($sounds) && (self::$new_order)) {
$plugin_dir = plugin_dir_url("mgd-woorestaurants-orderpickups/assets/") . "assets/tada.mp3";
echo "<script type='text/javascript'>let notifi= new Audio('$plugin_dir'); notifi.play();</script>";
}
I see the code in the page source, but no sound is every played. The console shows an error that reads:
"Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first."
I've read about the chrome policies for audio and video, and I cant seem to work out a way to get this audio file to play when a new order is found.
How do I set the browser to ignore its policies for this specific purpose?
Thanks in advance.
If you're using something like canvas and you're already depending on the browser supporting HTML5, you can use the audio element.
var win = new Audio('srcfile.wav');
if(playerWon === true){
win.play();
}
Audio in HTML5 is a minefield at the moment, with patchy format support.
We wrote a few blog posts about audio support in HTML5. This chart shows browser support: http://www.scirra.com/blog/44/on-html5-audio-formats-aac-and-ogg#chart
Surprisingly, IE doesn't support playback of WAVs. You should pick audio 2 formats to get coverage on all the platforms.
Beware using MP3's in games
It will cost you $2,500 per title in licensing fees if it has more than 5,000 distributions. http://www.scirra.com/blog/64/why-you-shouldnt-use-mp3-in-your-html5-games
We think the best combination to use is AAC/Ogg to cover all browsers.