我有一个奇怪的情况,音频html5只与移动游猎(iphone)。 如果我们尝试执行.play()INSIDE setTimeout,音频永远不会播放。
示例问题:
<script>
$(document).ready(function () {
var audio = $('<audio id="denied"><source src="sound/alarm.mp3" /></audio>');
audio.attr("loop", true)
audio.attr("playsinline", true);
audio.attr("controls", false);
audio.removeAttr("controls");
audio.appendTo("body");
$("#btn").on("click", function () {
window.setTimeout(function () {
audio.get(0).play(); //NOT WORKING HERE.
}, 3000);
});
});
</script>
但是如果执行play()不使用setTimeout Works:
<script>
$(document).ready(function () {
var audio = $('<audio id="denied"><source src="sound/alarm.mp3" /></audio>');
audio.attr("loop", true)
audio.attr("playsinline", true);
audio.attr("controls", false);
audio.removeAttr("controls");
audio.appendTo("body");
$("#btn").on("click", function () {
audio.get(0).play(); //WORKING
});
});
</script>