开发者

Stopping BGSOUND element on IE?

开发者 https://www.devze.com 2023-02-27 01:18 出处:网络
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

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.

H开发者_运维技巧ow 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'


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

document.getElementById('bgmusic').volume = -10000;


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 = '';
}


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.


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号