1

I've added background music to a website i'm building (don't kill me).
Whereas in IE the element is called BGSOUND, I tried to have a button to pause
the music, the button removes the entire element and re-adds it, however
turns out removing the element only restarts the music for IE.

How do I stop music being played by the BGSOUND element via javascript?

Current code:

<!--[if IE]>
    <BGSOUND id="bgmusic" SRC="<?php echo bloginfo('stylesheet_directory'); ?>/bgmusic.mp3" LOOP="true">
<![endif]-->

When I fire the command document.getElementById('bgmusic').stop()
I get Object doesn't support property or method 'stop'

Asaf
  • 8,106
  • 19
  • 66
  • 116
  • 1
    I'd still want to kill you for using , provided that you have no control on putting a background music at all =)) – Ege Özcan Apr 20 '11 at 10:27
  • that's why I want to know the command for stopping it - to create a control... Don't blame the developer, blame the client ;) – Asaf Apr 20 '11 at 10:29
  • Indeed: the [`bgSound`](http://msdn.microsoft.com/en-us/library/ms535198%28v=vs.85%29.aspx) object doesn't have a `stop` method. – Marcel Korpel Apr 20 '11 at 10:30
  • I blame the developer. The client doesn't know any better, you should. Instead of doing whatever the client asks, propose a better solution. – Sindre Sorhus Apr 20 '11 at 11:07
  • Not an answer, because this is just speculation, but have you tried putting the bgsound elment inside a iframe, then on stop, destroying the iframe? – Alohci Apr 20 '11 at 11:34

6 Answers6

3

I had a similar situation with the page of a client. I found that removing the src property of the bgsound elements is the best way to avoid the restart music problem in IE.

var bgsoundElems = document.getElementsByTagName('bgsound');
for(var i = 0; i < bgsoundElems.length; i++){
   bgsoundElems[i].src = '';
}
Altán
  • 51
  • 3
3

An ugly work around is by setting the volume to -10000:

document.getElementById('bgmusic').volume = -10000;
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
0

If you want to perform a stop(), you can just set src to an empty value. Works in IE11 (with IE8 emulation as well).

Giulio Paci
  • 303
  • 1
  • 7
0

I suggest using Flash instead. Not only will you have more control over the music, but it will work across browsers too. Today more users have Flash player than IE.

Vilx-
  • 104,512
  • 87
  • 279
  • 422
0

When user presses Pause: Remove the element and store it in a variable
When user presses Play : Get the element from the variable and add it to the DOM

sv_in
  • 13,929
  • 9
  • 34
  • 55
0

I recommend using SoundManager.

It will use HTML5 audio when it can, and will otherwise fall back to Flash. And it has a unified and easy API.

Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232