What trick will give most reliable/compatible sound alarm in a browser window for most browsers

安稳与你 提交于 2020-01-02 10:17:07

问题


I want to be able to play an alarm sound using Javascript in a browser window, preferably with the requirement for any browser plugins (Quicktime/Flash). I have been experimenting with the tag and the new Audio object in Javascript, but results are mixed:

As you can see, there is no variant that works on all browsers.

Do I miss a trick that is more cross-browser compatible?

This is my code:

// mp3 with Audio object
var snd = new Audio("/sounds/beep.mp3");snd.play();

// wav with Audio object
var snd = new Audio("/sounds/beep.wav");snd.play();

// mp3 with EMBED tag
$("#alarmsound").empty().append
('<embed src="/sounds/beep.mp3" autostart="true" loop="false" '+
 'volume="100" hidden="true" width="1" height="1" />');

// wav with EMBED tag
$("#alarmsound").empty().append
('<embed src="/sounds/beep.wav" autostart="true" loop="false" '+
 'volume="100" hidden="true" width="1" height="1" />');

}


回答1:


Have you tried HTML5's sound tag?




回答2:


Thank you for the table. I have the same problem. I don't want to use any flash, but it has to work on any platform. HTML 5 is only supported by newer Browsers and can't be used as a 'world wide' solution. There are still too many People using IE 6 :-) I used jQuery's browser property to manage the different objects:

if ($.browser.mozilla || $.browser.opera) {
  var snd = new Audio("beep.wav"); snd.play();
...
if ($.browser.msie) {
  var soundPlayer = $("<embed src='scripts/beep.mp3' hidden='true' autostart='true' loop='false' />");
  $("body").append(soundPlayer);
...


来源:https://stackoverflow.com/questions/3013973/what-trick-will-give-most-reliable-compatible-sound-alarm-in-a-browser-window-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!