起初你有一个错误的问题,因为你希望你的脚本在iOS上运行.因此,你在标题中的问题是无关紧要的.我们在iOS上有很多错误,iOS开发人员不想纠正它们.这是一个错误.写信给iOS支持并询问“怎么可能?”.
第二,你的帖子中有两个问题,因为你的脚本不能像你想要的那样在iOS上运行.
我为你写了解决方案.在音频元素加载元数据(第一个错误)之前,请确保您没有尝试调用音频.然后你有第二个错误:而不是:
if(audio.currentTime < start || audio.currentTime > end) //your mistake
你必须写:
if(audio.currentTime >= end)
解决方案(也适用于iOS)
var start = 1,
end = 1.3,
audio = document.getElementById('audio'),
button = document.getElementById('button'),
log = document.getElementById('log');
audio.addEventListener('timeupdate', function()
{
writeLog(audio.currentTime);
//your mistake: if(audio.currentTime < start || audio.currentTime > end)
if(audio.currentTime >= end)
{
audio.pause();
}
});
function writeLog(value)
{
var div = document.createElement('div');
div.innerHTML = value;
log.insertBefore(div, log.firstChild);
}
audio.addEventListener('loadedmetadata', function()
{
audio.pause();
button.removeAttribute('disabled');
});
button.addEventListener('click', function()
{
log.insertBefore(log.lastChild.cloneNode(!0), log.firstChild);
audio.currentTime = start;
audio.play();
});
#log
{
width:100%;
height:18em;
overflow-y:auto;
font:3em 'Courier New';
background:#069;
color:#7e0
}
#log div:last-child{display:none}