Javascript:如何防止页面加载时播放音频?

时间:2017-07-16 12:14:42

标签: javascript audio howler.js

我正在使用Dim strHostName As String Dim strIPAddress As String strHostName = System.Net.Dns.GetHostName() strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString() Dim con As New SqlConnection Dim cmd As New SqlCommand Dim MYFormName As String = Form.ActiveForm.Name Dim id As String = " / " + index_ar.ToolStripLabel2.Text Dim id1 As String = "" '" / " + index_en.ToolStripLabel2.Text Dim ctrlName As String = DirectCast(Sender, Control).Name 来实现我的音频。我所拥有的是非常简单但问题是在页面加载时播放音频,即使我没有这样说明。 这就是我所拥有的:

howler.js

这是我第一次这样做,所以我不确定是否有"停止"音频什么的。上面的代码在初始页面加载后按预期工作,但刷新页面意味着声音在每个初始加载时播放。我怎样才能防止这种情况发生?感谢。

2 个答案:

答案 0 :(得分:1)

来自变量hoversound

我猜你想要在元素悬停时发出声音(mouseenter)并在离开时停止(mouseleave

var hoverSound = new Howl({
  src: ['https://howlerjs.com/assets/howler.js/examples/player/audio/rave_digger.webm']
});

document.getElementById("player").addEventListener('mouseenter', function() {
  hoverSound.play();
  this.innerHTML = "Leave to Stop";
  this.className += "playing";
  console.log("Playing...");
});
document.getElementById("player").addEventListener('mouseleave', function() {
  hoverSound.stop();
  this.innerHTML = "Hover to Play";
  this.className = "";
  console.log("Stopped...");
});
#player {
  width: 100px;
  background-color: #dd5427;
  padding: 5px;
  color: white;
  font-weight: bold;
}

.playing {
  background-color: #689e0f !important
}
<script src="https://howlerjs.com/assets/howler.js/dist/howler.min.js"></script>
<div id="player">Hover to Play</div>

答案 1 :(得分:0)

使用cookie来存储'无论'变量。