问题
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'
回答1:
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.
回答2:
An ugly work around is by setting the volume to -10000:
document.getElementById('bgmusic').volume = -10000;
回答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 = '';
}
回答4:
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
回答5:
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.
回答6:
If you want to perform a stop(), you can just set src to an empty value. Works in IE11 (with IE8 emulation as well).
来源:https://stackoverflow.com/questions/5728677/stopping-bgsound-element-on-ie