首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >跨平台,跨浏览器的方式从Javascript播放声音?

跨平台,跨浏览器的方式从Javascript播放声音?
EN

Stack Overflow用户
提问于 2008-10-09 12:47:31
回答 9查看 60.2K关注 0票数 79

我正在编写一个dhtml应用程序,用于创建系统的交互式模拟。用于模拟的数据是从另一个工具生成的,并且已经有大量的遗留数据。

模拟中的一些步骤要求我们播放音频的“画外音”剪辑。我一直无法找到一种跨越多个浏览器的简单方法来完成此任务。

Soundmanager2非常接近我所需要的,但它只能播放mp3文件,而且遗留数据也可能包含一些.wav文件。

有没有人有其他库可以帮上忙?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2008-10-09 14:05:13

您将不得不包括一个插件,如Real Audio或QuickTime来处理.wav文件,但这应该可以工作…

代码语言:javascript
复制
//======================================================================
var soundEmbed = null;
//======================================================================
function soundPlay(which)
    {
    if (!soundEmbed)
        {
        soundEmbed = document.createElement("embed");
        soundEmbed.setAttribute("src", "/snd/"+which+".wav");
        soundEmbed.setAttribute("hidden", true);
        soundEmbed.setAttribute("autostart", true);
        }
    else
        {
        document.body.removeChild(soundEmbed);
        soundEmbed.removed = true;
        soundEmbed = null;
        soundEmbed = document.createElement("embed");
        soundEmbed.setAttribute("src", "/snd/"+which+".wav");
        soundEmbed.setAttribute("hidden", true);
        soundEmbed.setAttribute("autostart", true);
        }
    soundEmbed.removed = false;
    document.body.appendChild(soundEmbed);
    }
//======================================================================
票数 62
EN

Stack Overflow用户

发布于 2008-10-09 13:11:57

如果您正在使用Prototype,可以使用Scriptacars库has a sound API。jQuery appears to have a plugin也是。

票数 9
EN

Stack Overflow用户

发布于 2009-12-20 15:59:15

dacracots代码是干净的基本dom,但也许没有经过深思熟虑就写出来了?当然,您首先检查是否存在较早的嵌入,然后保存重复的嵌入创建行。

代码语言:javascript
复制
var soundEmbed = null;
//=====================================================================

function soundPlay(which)
{
    if (soundEmbed)
       document.body.removeChild(soundEmbed);
    soundEmbed = document.createElement("embed");
    soundEmbed.setAttribute("src", "/snd/"+which+".wav");
    soundEmbed.setAttribute("hidden", true);
    soundEmbed.setAttribute("autostart", true);
    document.body.appendChild(soundEmbed);
}

在扫描类似情况的解决方案时,我在这里遇到了这个想法。不幸的是,我的浏览器Mozilla/5.0 (X11;U;Linux i686;en-US;rv:1.9.0.15) Gecko/2009102814 Ubuntu/8.04 (hardy) Firefox/3.0.15在尝试时死掉了。

安装最新更新后,firefox仍然崩溃,opera仍然存活。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/187098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档