07.16
Some of the new exciting features of HTML5 are the audio and video tag. This could potentially, in the long term, replace flash on the video level. I wanted to use the audio tag in one of my experiemental project, there is little information about how to manipulate the audio tag, and tought it would be great to have a starter guide.
One thing clear is this is still an early implementation, which also means it is a bit buggy. I had a strange bug where if I had a song playing and had javascript throw errors at the same time (not related together), I could not terminate the song. Closing the tab would not stop it, I had to restart Firefox. Which lead me to think that the process is still not completely linked only to the tab (in Firefox at least).
I used jquery in my demo because the application I developing is wrapped around it, but this is not necessary.
View demo
(Only consisting of 5 buttons)
What I learned
The API is somewhat easy to work with, as it should be, play(), pause(), want to stop the volume? audioElement.volume=0. Want to start playing at 55 seconds? audioElement.currentTime = 55. Pretty cool stuff.
The default html audio player controls are added automatically with a simple: controls=”true”, it has good and bad sides. You don’t have to worry about it, but at the same time, for now, you cannot modify the player style. Also when you right click you have a nice contextual menu where you can save the file in ff3.5.
A normal audio tag
Your browser does not support the audio tag.
Add and play a sound via JavaScript
-
var audioElement = document.createElement(‘audio’);
-
audioElement.setAttribute(’src’, ‘loading.ogg’);
-
audioElement.play();
Get the song filepath and duration
-
audioElement.src;
-
audioElement.duration;
Load a sound
-
var audioElement = document.createElement(‘audio’);
-
audioElement.setAttribute(’src’, ‘Mogwai2009-04-29_acidjack_t16.ogg’);
-
audioElement.load()
-
audioElement.addEventListener(“load”, function() {
-
audioElement.play();
-
$(“.duration span”).html(audioElement.duration);
-
$(“.filename span”).html(audioElement.src);
-
}, true);
Stop a song
-
audioElement.pause();
Change volume
-
audioElement.volume=0;
Play at exactly 35 seconds in the song
-
audioElement.currentTime=35;
-
audioElement.play();
The bad parts
No stop(), only pause(), I really do not know what stopped them to put stop() in the API.
No way to get the songs name.
The only accessible format are ogg and wav, no mp3 love. It also means that you will have to convert all your sounds in ogg.
As I said this is a bit buggy but understandable, the specs are not even finished.
Microsoft will probably implement the audio and video tag in 2022.
The good parts
The API is easy to use, you can have a song playing in no time.
You do not need to parse the audio object in the HTML body, you can have it played directly in your script.
In the end, I’m really happy on how this turned out.
Download the source code View demo
Tested on:
Firefox 3.5
Ressources
http://hacks.mozilla.org/2009/06/exploring-music-audio/
https://developer.mozilla.org/en/Using_audio_and_video_in_Firefox
http://webkit.org/blog/140/html5-media-support/
暂无回复
添加回复