Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Looping an audio file that plays on button click?

    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":

    Code:
    <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!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    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.

  • Users who have thanked Philip M for this post:

    FaustWolf (09-03-2007)

  • #3
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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):

    Code:
    <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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •