浏览器中的Javascript音频支持-特质

时间:2018-08-01 06:51:53

标签: javascript html5 audio

我浏览了这个网站以及其他网站,找不到关于这种特质的任何解释或解决方案。

尽管在这些不同的站点上有什么说法,但是Javascript和HTML5 DOM音频似乎并不能在所有浏览器上正常工作,正如在各个站点上的各种讨论所表明的那样。

<html>
<head>
</head>
<body>

<audio id="buttonAudio" src="buttonClick.wav"></audio>

<button onclick="newGame00()">New Game 00</button>
<button onclick="newGame01()">New Game 01</button>
<button onclick="newGame02()">New Game 02</button>
<button onclick="newGame03()">New Game 03</button>
<button onclick="newGame04()">New Game 04</button>
<button onclick="newGame05()">New Game 05</button>
<button onclick="newGame06()">New Game 06</button>

<script>

// W3 Schools (https://www.w3schools.com/html/html5_audio.asp) says Firefox has
//   supported HTML5 audio since version 3.5, and Internet Explorer has supported
//   it since version 9.0.

// W3 Schools also (https://www.w3schools.com/jsref/met_audio_play.asp) says 
//   Firefox supports the HTML5 DOM Audio object, and Internet Explorer has done
//   so since version 9.0. In fact, this W3 Schools page specifically recommends
//   the method I've implemented via the "New Game 06" button.

// Nevertheless:

// I've tried all six of these methods for getting a button click in javascript.
//   None of them work in version 61.0.1 (64-bit) of Mozilla Firefox.
//   None of them work in version 11.0.9600.19080 of Internet Explorer.
//   All six of them work in version 41.16299.371.0 of Edge.
//   All six of them work in version 67.0.3396.99 (64-bit) of Google Chrome.
//   All six of them work in version 54.0.2952.64 of Opera.
//   All six of them work in version 19.0.2.0 (32-bit) of Slimjet.

var buttonClick = new Audio("buttonClick.wav");

var x = document.getElementById('buttonAudio');

function newGame00() {
    buttonClick.play();
}

function newGame01() {
    buttonAudio.play();
}

function newGame02() {
    doit02();
}

function newGame03() {
    doit03();
}

function newGame04() {
    document.getElementById('buttonAudio').play();
}

function newGame05() {
    doit05();
}

function newGame06() {
    x.play();
}

async function doit02() {
    buttonClick.play();
}

async function doit03() {
    buttonAudio.play();
}

async function doit05() {
    document.getElementById('buttonAudio').play();
}

</script>
</body>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Mozilla支持论坛的主持人

cor-el为我解决了此问题。 Firefox不支持所有.wav选项。 Audacity将其保存为44100Hz。根据他的建议,我将其重新设置为48000Hz。

现在,它可以在Firefox中运行,并且可以继续在Edge,Chrome,Opera和Slimjet中运行。

当然,它在IE中仍然不起作用。 (叹)。显然,IE根本不支持.wav文件。似乎也不支持.mp3或.ogg文件。

相关问题