暂停和取消暂停功能中的音频
Pausing and Unpausing Audio in A Function
我的网站在加载时播放音乐,但我也希望能够点击按钮暂停或取消暂停音乐。特别是对于自动播放或.onload不起作用的OSX。然而,我不确定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>
在我实现了第二个功能(Pause)后,第一个功能停止工作。现在没有声音播放,与点击图片无关。
几个问题。
看起来您忘记在函数名称后添加括号()
,您需要这些括号来调用函数。
function Pause {
if (audio.play = false){
audio.play();
}
else {
audio.pause();
}
}
应该是这样的:
function Pause() {
if (audio.play = false){
audio.play();
}
else {
audio.pause();
}
}
您还忘记将它们添加到onclick定义中。
<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;">
你的代码整体应该是这样的:
<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>
相关文章:
- 如何检测我的音频流是:正在播放、暂停还是已完成
- 带有音频标签的多个播放/暂停按钮
- 单击时暂停音频并更改按钮的图像
- 在嵌入标签中开始/暂停音频?(IE8+)
- 创建一个只有播放和暂停的Javascript音频播放列表
- 暂停和取消暂停功能中的音频
- <音频>播放/暂停按钮更改
- 使用HTML5和JavaScript播放/暂停音频按钮
- 使用 HTML5 和 JavaScript 播放/暂停音频按钮
- 在 Div 悬停时暂停音频
- 音轨 JS 功能无法播放或暂停音频
- Javascript禁止点击/暂停音频的暂停按钮
- 使用javascript命令播放/暂停音频文件
- 播放或暂停音频时切换图标
- 如果使用Knockout注册了暂停事件,为什么无法暂停音频
- Youtube JavaScript API -暂停音频时,无论是Youtube视频播放
- 使用JavaScript播放或暂停音频文件(onkeydown)
- 通过获取.attr,使用JavaScript / jquery播放/暂停音频文件
- JavaScript播放/暂停音频按钮,更改类并创建自动循环
- 播放和暂停音频onclick由js库