在 IE 上停止 BGSOUND 元素?

发布于 2024-11-02 04:50:19 字数 490 浏览 7 评论 0原文

我已将背景音乐添加到我正在构建的网站中(别杀了我)。
而在 IE 中,该元素称为 BGSOUND,我尝试使用一个按钮来暂停
音乐,按钮会删除整个元素并重新添加它,但是
结果删除该元素只会重新启动 IE 的音乐。

如何通过 javascript 停止 BGSOUND 元素播放音乐?

当前代码:

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

当我触发命令时 document.getElementById('bgmusic').stop()
我得到对象不支持属性或方法“停止”

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挽手叙旧 2024-11-09 04:50:19

一个丑陋的解决方法是设置 音量到-10000:

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

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

document.getElementById('bgmusic').volume = -10000;
好听的两个字的网名 2024-11-09 04:50:19

我在客户页面上遇到了类似的情况。
我发现 删除 src 属性 bgsound 元素是避免 IE 中重新启动音乐问题的最佳方法。

var bgsoundElems = document.getElementsByTagName('bgsound');
for(var i = 0; i < bgsoundElems.length; i++){
   bgsoundElems[i].src = '';
}

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 = '';
}
夜无邪 2024-11-09 04:50:19

我建议使用 Flash。您不仅可以更好地控制音乐,而且还可以跨浏览器工作。如今,拥有 Flash 播放器的用户多于 IE

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.

¢蛋碎的人ぎ生 2024-11-09 04:50:19

当用户按下暂停时:删除元素并将其存储在变量中
当用户按下 Play 时:从变量中获取元素并将其添加到 DOM

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

烟花易冷人易散 2024-11-09 04:50:19

我建议使用 SoundManager

它将尽可能使用 HTML5 音频,否则将回退到 Flash。它有一个统一且简单的API

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.

浊酒尽余欢 2024-11-09 04:50:19

如果要执行 stop(),只需将 src 设置为空值即可。适用于 IE11(也可使用 IE8 仿真)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文