使用 jQuery 1.4 跨平台、跨浏览器播放声音的方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2769077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:15:01  来源:igfitidea点击:

Cross-platform, cross-browser way to play sound using jQuery 1.4?

jqueryaudio

提问by Erin

I'm trying to get jQuery to play a sound on element hover/click. (It's like a flash website without flash)

我正在尝试让 jQuery 在元素悬停/单击时播放声音。(就像一个没有flash的flash网站)

I've tried the methods recommended in Cross-platform, cross-browser way to play sound from Javascript?, the jQuery Sound plugin and a couple of other tutorials without any success. I'm assuming this is because they haven't been updated since 2008.

我已经尝试过跨平台、跨浏览器方式中推荐的方法来从 Javascript 播放声音?,jQuery Sound 插件和其他几个教程没有任何成功。我假设这是因为它们自 2008 年以来就没有更新过。

Does anyone have any recommendations?

有人有什么建议吗?

回答by Delan Azabani

Don't need any of those. The HTML tag, combined with JavaScript, will do the trick.

不需要任何这些。HTML 标记与 JavaScript 相结合,可以解决问题。

<audio id="soundHandle" style="display: none;"></audio>
<script>
  soundHandle = document.getElementById('soundHandle');
  soundHandle.src = '/blah/blah.mp3';
  soundHandle.play();
</script>

回答by Jakub Hampl

Actually it's impossible to do in a truly cross-browser compliant way as for example iPhones will play sound only on a dedicated player "page". However some of the plugins in the other answers should do the job for most (desktop) browsers.

实际上,不可能以真正跨浏览器兼容的方式进行,例如 iPhone 只会在专用的播放器“页面”上播放声音。然而,其他答案中的一些插件应该可以为大多数(桌面)浏览器完成这项工作。

回答by ThinkFloyd

Audio tag did not work for me in some browsers so this is how I got it done:

音频标签在某些浏览器中对我不起作用,所以我是这样完成的:

function playBuzzer(){
        $("body").append("<embed src='/web/sounds/Buzz.mp3' autostart='true' loop='false' width='2' height='0'></embed>");
}

Whenever I want to play sound (onmouseover, onclick, onload...) I call this function.

每当我想播放声音(onmouseover、onclick、onload...)时,我都会调用这个函数。

回答by FDisk

try this: http://swaggplayer.no.de/demosit using the flash to play a sound or this one http://www.schillmania.com/projects/soundmanager2/

试试这个:http: //swaggplayer.no.de/demos它使用闪光灯播放声音或这个 http://www.schillmania.com/projects/soundmanager2/

回答by FDisk

http://codecanyon.net/item/fancy-music-player-v20-jquery-plugin/498071?ref=1stwebdesigner

http://codecanyon.net/item/fancy-music-player-v20-jquery-plugin/498071?ref=1stwebdesigner

Player works like this - first it detects if Flash Player 9 or higher is installed. If yes a hidden flash movie(swf) will be used, that communicates via javascript with the HTML user interface. If no Flash Player is installed, the new HTML5 audio engine will be used, which is supported by all iToys(iPhone, iPad etc.).

Player 的工作方式如下 - 首先它检测是否安装了 Flash Player 9 或更高版本。如果是,将使用隐藏的 Flash 电影(swf),通过 javascript 与 HTML 用户界面进行通信。如果未安装 Flash Player,将使用新的 HTML5 音频引擎,所有 iToys(iPhone、iPad 等)都支持该引擎。