How to play audio?
If you don't want to mess with HTML elements:
var audio = new Audio('audio_file.mp3');audio.play();
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.
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.
http://www.schillmania.com/projects/soundmanager2/
SoundManager 2 provides a easy to use API that allows sound to be played in any modern browser, including IE 6+. If the browser doesn't support HTML5, then it gets help from flash. If you want stricly HTML5 and no flash, there's a setting for that, preferFlash=false
It supports 100% Flash-free audio on iPad, iPhone (iOS4) and other HTML5-enabled devices + browsers
Use is as simple as:
<script src="soundmanager2.js"></script><script> // where to find flash SWFs, if needed... soundManager.url = '/path/to/swf-files/'; soundManager.onready(function() { soundManager.createSound({ id: 'mySound', url: '/path/to/an.mp3' }); // ...and play it soundManager.play('mySound'); });</script>
Here's a demo of it in action: http://www.schillmania.com/projects/soundmanager2/demo/christmas-lights/