在功能中暂停和取消暂停音频

时间:2016-04-20 02:40:06

标签: javascript

我的网站正在播放音乐,但我也希望能够点击按钮暂停或取消暂停音乐。特别是对于OSX,其中autoplay或.onload不起作用。但是,我不确定我在onclick事件中出错了。

<script type="text/javascript">
var audio = new Audio('/Sounds/Macintosh.mp3');
function detectmob() { 
         if( navigator.userAgent.match(/Android/i) //Checks if on mobile
         || navigator.userAgent.match(/webOS/i)
         || navigator.userAgent.match(/iPhone/i)
         || navigator.userAgent.match(/iPad/i)
         || navigator.userAgent.match(/iPod/i)
         || navigator.userAgent.match(/BlackBerry/i)
         || navigator.userAgent.match(/Windows Phone/i)
         ){
              return true;
          }
         else {
              var audio = new Audio('/Sounds/Macintosh.mp3');
              audio.play(); 
              }
}

function Pause {
    if (audio.play = false){
    audio.play();
    }
    else {
    audio.pause();
    }


}
window.onload = detectmob;
</script>
<center> <input type="image" onclick="Pause" src="/images/button.jpg" style="width:750px;height:400px;"></center>

实现第二个功能(暂停)后,第一个功能停止工作。现在没有声音播放,与点击图片无关。

1 个答案:

答案 0 :(得分:0)

有几个问题。

看起来你忘了在函数名后添加括号public void OnSelectedIndexChanged(object sender, EventArgs e) { int selectedIndex = myListView.SelectedIndex; PageViewItemModel model = myListView.Items[selectedIndex]; myTableLayOutPanel.Controls.Add(model, 0, 1); } ,你需要调用函数。

()

应该是这样的:

function Pause {
    if (audio.play = false){
    audio.play();
    }
    else {
    audio.pause();
    }
}

您也忘了将它们添加到onclick定义中。

function Pause() {
    if (audio.play = false){
    audio.play();
    }
    else {
    audio.pause();
    }   
}

应该是:

<input type="image" onclick="Pause" src="/images/button.jpg" style="width:750px;height:400px;">

您的代码应如下所示:

<input type="image" onclick="Pause()" src="/images/button.jpg" style="width:750px;height:400px;">