从 Javascript 播放声音的跨平台、跨浏览器方式? [英] Cross-platform, cross-browser way to play sound from Javascript?

查看:26
本文介绍了从 Javascript 播放声音的跨平台、跨浏览器方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

模拟中的某些步骤要求我们播放音频的画外音"剪辑.我一直无法找到一种简单的方法来跨多个浏览器完成此操作.

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 非常接近我的需要,但它只能播放 mp3 文件,并且遗留数据也可能包含一些 .wav 文件.

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.

有没有其他图书馆可以提供帮助?

Does anyone have any other libraries that might help?

推荐答案

你必须包含一个像 Real Audio 或 QuickTime 这样的插件来处理 .wav 文件,但这应该可以工作...

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

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

这篇关于从 Javascript 播放声音的跨平台、跨浏览器方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆