要在網頁裡放音樂,寫法會依檔案副檔名而異。以下依格式整理對應的嵌入語法。
先統一套用語:「音樂檔案與路徑」指待播放檔案的位置,可填寫完整絕對路徑,也可使用站內相對路徑;width_num 與 height_num 分別代表播放器寬度與高度的數值。
<embed src="音樂檔案與路徑" type="audio/midi" hidden="false" autostart="true" loop="true" height="height_num" width="width_num"></embed>
<embed controller="true" width="width_num" height="height_num" src="音樂檔案與路徑" border="0" pluginspage="https://www.apple.com/quicktime/download/indext.html"></embed>
<embed src="音樂檔案與路徑" border="0" width="width_num" height="height_num" type="application/x-mplayer2"></embed>
<embed src="音樂檔案與路徑" pluginspage="https://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="width_num" height="height_num" play="true" loop="true" quality="high" scale="showall" menu="true"></embed>
<embed src="音樂檔案與路徑" border="0" width="width_num" height="height_num" type="application/x-mplayer2"></embed>
<embed controller="true" width="width_num" height="height_num" src="音樂檔案與路徑" border="0" pluginspage="https://www.apple.com/quicktime/download/indext.html"></embed>
這種格式需要 object 與 embed 搭配使用:
<OBJECT classid="..." height="height_num">
<PARAM NAME="CONTROLS" VALUE="ImageWindow">
<PARAM NAME="AUTOSTART" Value="true">
<PARAM NAME="SRC" VALUE="音樂檔案與路徑">
<embed height="height_num" width="width_num" controls="ImageWindow" src="音樂檔案與路徑" type="audio/x-pn-realaudio-plugin" autostart="true" nolabels="0" autogotourl="-1"></OBJECT>
如果不需要顯示播放器介面,可以用最精簡的寫法:
<bgsound src="音樂檔案與路徑" loop="1">
其中 loop 後方的數字是循環次數,填 -1 代表無限循環。
上述寫法都依賴瀏覽器外掛(plugin),在當代瀏覽器中多已失效,Flash 與 RealPlayer 支援也已終止。若今天要實作同樣的功能,建議改用 HTML5 的 <audio> 元素搭配 <source>,並用 controls、loop、autoplay 屬性控制行為。另外要留意:自動播放音樂容易干擾使用者,多數現代瀏覽器已預設阻擋,實務上應交由使用者點擊觸發。