...

View Full Version : Looping an audio file that plays on button click?



FaustWolf
09-02-2007, 07:06 PM
If possible, I'd like to loop an audio file that plays when a button is pressed in an html document I've created. I'm currently using the following JavaScript code to play a file called "Carnival of Lights.mp3":


<BGSOUND id=BGSOUND_ID src="">
<bgsound id="bgsound_id" loop=1 src="">
<embed name="guitar" src="" loop=true autostart=false hidden=true mastersound>

<script language="JavaScript">
<!--
ver=parseInt(navigator.appVersion)
ie4=(ver>3 && navigator.appName!="Netscape")?1:0
ns4=(ver>3 && navigator.appName=="Netscape")?1:0
ns3=(ver==3 && navigator.appName=="Netscape")?1:0

function playSound() {
if (ie4) document.all['BGSOUND_ID'].src='Carnival of Lights.mp3';
if ((ns4||ns3)
&& navigator.javaEnabled()
&& navigator.mimeTypes['audio/x-midi']
&& self.document.guitar.IsReady()
)
{
self.document.guitar.play()
}
}
//-->
</script>

<form name=myform>
<input type=button value="Play Sound" onClick="playSound()">
</form>

Works like a charm, except the mp3 doesn't loop. Any ideas on what code I should add/change to add audio looping functionality to my html document?

Thanks in advance!

Philip M
09-03-2007, 08:25 AM
embed src="MusicFile.mp3" autostart=true repeat=true>

This seems to work Opera, FireFox and IE.

But the embed tag has never been part of the HTML specification and never will be, so cannot be guaranteed to work in all browsers now or in future. And bgsound is MSIE specific. Make loop = infinite.

FaustWolf
09-03-2007, 04:24 PM
Philip M, you're officially my hero for helping out with this. I've got it working now thanks to your suggestions. For future reference in case anybody else has the same kind of trouble I had, this is the working code (tested only on IE 7):


<BGSOUND id=BGSOUND_ID loop="infinite" src="" >
<bgsound id="bgsound_id" loop="infinite" src="">
<embed name="guitar" src="Carnival of Lights.mp3" loop=true repeat=true autostart=false hidden=true mastersound>

<script language="JavaScript">
<!--
ver=parseInt(navigator.appVersion)
ie4=(ver>3 && navigator.appName!="Netscape")?1:0
ns4=(ver>3 && navigator.appName=="Netscape")?1:0
ns3=(ver==3 && navigator.appName=="Netscape")?1:0

function playSound() {
if (ie4) document.all['BGSOUND_ID'].src='Carnival of Lights.mp3';
if ((ns4||ns3)
&& navigator.javaEnabled()
&& navigator.mimeTypes['audio/x-midi']
&& self.document.guitar.IsReady()
)
{
self.document.guitar.play()
}
}
//-->
</script>

<form name=myform>
<input type=button value="Play Sound" onClick="playSound()">
</form>

Thanks a million!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum