iOS浏览器(任何浏览器)将无法播放用户事件引发的UI声音

时间:2019-02-16 18:44:30

标签: javascript audio autoplay

我已经尝试了几种方法,但是都没有用。我曾尝试在iOS上运行的Chrome,Safari和Firefox上尝试过,但所有声音都无声。这是我在所有Web应用程序上使用的sounds.js库的代码:

// Sounds
var silenceEle;
var sndDir = "/sounds/";
var sndSilence = sndDir + "silence.wav";
var sndIntro = sndDir + "intro.wav";
var sndType = sndDir + "type.wav";
var sndAlert1 = sndDir + "alert1.wav";
var sndAlert2 = sndDir + "alert2.wav";
var sndAlert3 = sndDir + "alert3.wav";
var sndDing = sndDir + "ding.wav";
var sndButton = sndDir + "button.wav";
var sndMessage = sndDir + "message.wav";
var sndError = sndDir + "error.wav";
var sndSuccess = sndDir + "success.wav";
var sndSlide = sndDir + "slide.wav";
var sndAHAHAH = sndDir + "nedry.wav";

$(document).ready(function () {
  silenceEle = document.createElement('audio');
  silenceEle.setAttribute('src', sndSilence);
  silenceEle.setAttribute('autoplay', true);
});

function playSound(snd)
{
  var old = document.getElementsByClassName('soundsJS');
  while (old[0])
  {
    old[0].parentNode.removeChild(old[0]);
  }
  var ele = document.createElement('audio');
  ele.setAttribute('src', snd);
  ele.setAttribute('autoplay', true);
  ele.setAttribute('class', 'soundsJS');
  silenceEle.play();
  ele.play();
}

我还尝试将其放在HTML模板的正文中(顶部):

<iframe src="/sounds/silence.wav" allow="autoplay" id="audio" style="display:none"></iframe>

我在另一篇文章中读到了有关此黑客的信息,但没有声音发出。我仅在用户启动事件(例如playSound())时运行target.click(),但仍然无法播放任何内容!有什么想法吗?

0 个答案:

没有答案