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

时间:2022-05-27 02:57:06

I am writing a dhtml application that creates an interactive simulation of a system. The data for the simulation is generated from another tool, and there is already a very large amount of legacy data.

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

Some steps in the simulation require that we play "voice-over" clips of audio. I've been unable to find an easy way to accomplish this across multiple browsers.

模拟中的一些步骤要求我们播放音频的“画外音”剪辑。我无法在多个浏览器中找到一种简单的方法来实现这一目标。

Soundmanager2 comes pretty close to what I need, but it will only play mp3 files, and the legacy data may contain some .wav files as well.

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

Does anyone have any other libraries that might help?

有没有人可以帮助其他任何图书馆?

9 个解决方案

#1


62  

You will have to include a plug-in like Real Audio or QuickTime to handle the .wav file, but this should work...

您将必须包含Real Audio或QuickTime等插件来处理.wav文件,但这应该有效...

//======================================================================
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);
    }
//======================================================================

#2


10  

If you're using Prototype, the Scriptaculous library has a sound API. jQuery appears to have a plugin, too.

如果您正在使用Prototype,则Scriptaculous库具有声音API。 jQuery似乎也有一个插件。

#3


9  

dacracots code is clean basic dom, but perhaps written without a second thought? Of course you check the existance of an earlier embed first, and save the duplicate embed creation lines.

dacracots代码是干净的基本dom,但也许没有第二个想法写?当然,您先检查先前嵌入的存在,然后保存重复的嵌入创建行。

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);
}

Came across the thoughts here while scanning for a solution for somewhat similar situation. Unfortunately my browser Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.15) Gecko/2009102814 Ubuntu/8.04 (hardy) Firefox/3.0.15 dies when trying this.

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

After installing latest updates, firefox still crashes, opera keeps alive.

安装最新更新后,Firefox仍然崩溃,歌剧仍然活着。

#4


8  

I believe that the simplest and most convenient way would be to play the sound using a small Flash clip. I appreciate it's not a JavaScript solution but it IS the easiest way to achieve your goal

我相信最简单和最方便的方法是使用一个小Flash片段播放声音。我很欣赏它不是JavaScript解决方案,但它是实现目标的最简单方法

Some extra links from the previous similar question:

以前类似问题的一些额外链接:

#5


6  

I liked dacracot's answer... here is a similar solution in jQuery:

我喜欢dacracot的答案......这是jQuery中的类似解决方案:

function Play(sound) {
    $("#sound_").remove()
    $('body').append('<embed id="sound_" autostart="true" hidden="true" src="/static/sound/' + sound + '.wav" />');
}

#6


5  

You can use the HTML5 <audio> tag.

您可以使用HTML5

#7


4  

If you are using Mootools, there is the MooSound plugin.

如果你使用的是Mootools,那就是MooSound插件。

#8


3  

<audio controls>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

See http://www.w3schools.com/html/html5_audio.asp for more details.

有关详细信息,请参阅http://www.w3schools.com/html/html5_audio.asp。

#9


1  

There is a far more simpler solution to this rather than having to resort to plug-ins. IE has it's own bgsound property and there is another way you can make it work for all other browsers. Check out my tutorial here = http://www.andy-howard.com/how-to-play-sounds-cross-browser-including-ie/index.html

有一个更简单的解决方案,而不是诉诸插件。 IE拥有自己的bgsound属性,还有另一种方法可以让它适用于所有其他浏览器。看看我的教程= http://www.andy-howard.com/how-to-play-sounds-cross-browser-including-ie/index.html

#1


62  

You will have to include a plug-in like Real Audio or QuickTime to handle the .wav file, but this should work...

您将必须包含Real Audio或QuickTime等插件来处理.wav文件,但这应该有效...

//======================================================================
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);
    }
//======================================================================

#2


10  

If you're using Prototype, the Scriptaculous library has a sound API. jQuery appears to have a plugin, too.

如果您正在使用Prototype,则Scriptaculous库具有声音API。 jQuery似乎也有一个插件。

#3


9  

dacracots code is clean basic dom, but perhaps written without a second thought? Of course you check the existance of an earlier embed first, and save the duplicate embed creation lines.

dacracots代码是干净的基本dom,但也许没有第二个想法写?当然,您先检查先前嵌入的存在,然后保存重复的嵌入创建行。

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);
}

Came across the thoughts here while scanning for a solution for somewhat similar situation. Unfortunately my browser Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.15) Gecko/2009102814 Ubuntu/8.04 (hardy) Firefox/3.0.15 dies when trying this.

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

After installing latest updates, firefox still crashes, opera keeps alive.

安装最新更新后,Firefox仍然崩溃,歌剧仍然活着。

#4


8  

I believe that the simplest and most convenient way would be to play the sound using a small Flash clip. I appreciate it's not a JavaScript solution but it IS the easiest way to achieve your goal

我相信最简单和最方便的方法是使用一个小Flash片段播放声音。我很欣赏它不是JavaScript解决方案,但它是实现目标的最简单方法

Some extra links from the previous similar question:

以前类似问题的一些额外链接:

#5


6  

I liked dacracot's answer... here is a similar solution in jQuery:

我喜欢dacracot的答案......这是jQuery中的类似解决方案:

function Play(sound) {
    $("#sound_").remove()
    $('body').append('<embed id="sound_" autostart="true" hidden="true" src="/static/sound/' + sound + '.wav" />');
}

#6


5  

You can use the HTML5 <audio> tag.

您可以使用HTML5

#7


4  

If you are using Mootools, there is the MooSound plugin.

如果你使用的是Mootools,那就是MooSound插件。

#8


3  

<audio controls>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

See http://www.w3schools.com/html/html5_audio.asp for more details.

有关详细信息,请参阅http://www.w3schools.com/html/html5_audio.asp。

#9


1  

There is a far more simpler solution to this rather than having to resort to plug-ins. IE has it's own bgsound property and there is another way you can make it work for all other browsers. Check out my tutorial here = http://www.andy-howard.com/how-to-play-sounds-cross-browser-including-ie/index.html

有一个更简单的解决方案,而不是诉诸插件。 IE拥有自己的bgsound属性,还有另一种方法可以让它适用于所有其他浏览器。看看我的教程= http://www.andy-howard.com/how-to-play-sounds-cross-browser-including-ie/index.html