NetBulge.com

HTML Music Code

This article can be found online:
http://www.netbulge.com/index.php?session=0&action=read&click=open&article=1140789557


Inserting an audio file into a web document should be a somewhat uneventful task. Actually, if you do not care about standards it is rather simple since the non-standard <embed> tag works very well in 95% of browsers. It only gets complicated when trying to follow the W3C specification. In this article I provide both methods and you can decide for yourself.

Inserting an mp3 file using the embed tag is as easy as everything should be:

<embed src="music.mp3" width="200" height="20" autostart="true" loop="true">

The funny thing is the embed tag, although widely supported by most browsers and overall the safest bet for inserting music in a page, has never been part of the standard definition of HTML. In any event, if you, dear visitor, do not care about these so called HTML standards and are only looking for a simple and effective way to insert an mp3 file in your web page, then look no further; use the above code with the embed tag and disregard the rest of this monologue.

Now, back to my rambling, the standard method for inserting multimedia into a webpage is the object tag. The problem is that, although it was originally a Microsoft tag, Internet Explorer disregards the recommended use. The following html code works correctly in Firefox:

<object data="music.mp3" type="audio/x-mpeg">
</object>

Yet IE ignores this simple and clear request.

Microsoft likes its multimedia objects like it likes its coffee, bizarre and unapologetic. So, in order to make the audio file play in IE we must first declare the control that will handle it; the first choice being the dear old Windows Media Player, a bulky tool that a friend of mine affectionately calls the "Memory Eating Monster". But to follow this requirement correctly we must use the lengthy and highly proprietary ActiveX ID system that of course is only recognized in Windows and will crash and burn on any other platform:

<OBJECT ID="MediaPlayer1" WIDTH=320 HEIGHT=240
CLASSID="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95"
CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/
nsmp2inf.cab#Version=5,1,52,701"
standby="Loading Microsoft Windows Media Player components..."
type="application/x-oleobject">
<Param Name="FileName" Value="music.mp3">
</OBJECT>

Cute isn't it? So, we are trapped either:

  1. Neglecting one browser's method in favor of the other
  2. Using both methods on the same page and hoping for the best
  3. Hacking this up into making it behave our way

Quite frankly, with the ready availability of the embed tag for inserting music into a web page, I really don't feel like considering any of the <object> related alternatives. I don't normally insert audio in my HTML pages anyway, and I'm sure that if I ever have the need for it I'll be able to live with my page not validating or use a more extensible tool such as Flash.



Author: Esopo
From NetBulge.com