在 iOS11 safari 浏览器(iOS10 及以下版本有效)上,如果我离开并返回,有时音频文件无法播放。如果我不断地反复敲击,它最终会播放 - 但它会分批所有播放调用并尝试一次播放。
我创建了一个非常简单的测试页面。有谁知道为什么?
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,minimum-scale=1,maximum-scale=1"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no" />
<style>
button { font-size:2em; }
</style>
<script>
var g_context, g_buffer;
function play() {
if (g_buffer) {
var source = g_context.createBufferSource();
source.buffer = g_buffer;
source.connect(g_context.destination);
source.start(0);
}
}
function ready(){
g_context = new webkitAudioContext();
var req = new XMLHttpRequest();
req.open("GET", "<*** insert your sound file path here ***>", true);
req.responseType = "arraybuffer";
req.onload = function(){
g_context.decodeAudioData(req.response,function(buffer){
g_buffer = buffer;
}, function(e){});
};
req.onerror = function(e){
console.log("fail to load", e);
};
try{req.send()}
catch(e){}
}
</script>
</head>
<body onload="ready()">
<button onclick="play()">play</button>
</body>
</html>