【发布时间】:2016-11-25 16:22:08
【问题描述】:
我想在页面加载时播放背景音乐。
我的代码在除 Safari 之外的所有浏览器上都能正常运行。
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);
【问题讨论】:
-
什么版本的 Safari?
-
safari 5.1.7 windows 10 版本
-
根据 this page 网络音频 API 在版本 6 之前的 safari 中不可用 - 但是自 3.1 版以来 safari 已支持
<audio>标签 - 也许您需要重新考虑如何添加Audio构造函数不存在时的音频 -
有什么建议吗??只想播放背景音乐的暂停按钮
标签: javascript web safari cross-browser mozilla