我想在页面上播放背景音乐。
除了Safari之外,我的代码在所有浏览器上都运行良好。
狩猎表演。
Undefined is not constructor(evaluating new audio())
如何在Safari上修复此错误?
var audio, playbtn, mutebtn, seek_bar;
function initAudioPlayer(){
audio = new Audio();
audio.src = "html/audio/di-evantile_behind-your-dream.mp3";
audio.loop = true;
audio.play();
// Set object references
playbtn = document.getElementById("playpausebtn");
mutebtn = document.getElementById("mutebtn");
// Add Event Handling
playbtn.addEventListener("click",playPause);
mutebtn.addEventListener("click", mute);
// Functions
function playPause(){
if(audio.paused){
audio.play();
playbtn.style.background = "url(html/images/pause.png) no-repeat";
} else {
audio.pause();
playbtn.style.background = "url(html/images/play.png) no-repeat";
}
}
function mute(){
if(audio.muted){
audio.muted = false;
mutebtn.style.background = "url(html/images/speaker.png) no-repeat";
} else {
audio.muted = true;
mutebtn.style.background = "url(html/images/speaker_muted.png) no-repeat";
}
}
}
//play music on page load
window.addEventListener("load", initAudioPlayer);
发布于 2016-11-21 21:38:14
您的JavaScript中似乎有一个奇怪的评论。为了让您的脚本在我的机器上运行,我不得不删除"##标题##“。
关于Safari:
早在2012年就曾是停产。如果各种HTML5产品在Windows版本中都不起作用,我就不会感到惊讶了。
这是为我工作的Mac系统与Safari9.1.1 (11601.6.17).
我还尝试使用Safari for Windows,正如您所描述的那样,我可以确认它不工作,因为不支持Audio对象。
https://stackoverflow.com/questions/40734201
复制