大佬教程收集整理的这篇文章主要介绍了javascript – IOS上的HTML5音频:currentTime如何小于初始值?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我注意到,在很少的早期更新事件中,有时currentTime小于其初始值,尽管(显然)没有“倒带”动作.
var start = 1; var end = 1.3; var audio = document.getElementById('audio'); audio.addEventListener('timeupdate',function () { console.log(audio.currentTimE); if (audio.currentTime < start || audio.currentTime > end) { audio.pause(); } }); audio.currentTime = start; audio.play();
例如,控制台日志的输出可以是:
1 0.85 0.85 1 1.02 ...
这是an example.
使用iOS 11.4.1在iPad上测试.此问题仅在非常短的时间范围内出现~0.3秒.
第二,你的帖子中有两个问题,因为你的脚本不能像你想要的那样在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}
<!-- For this link from your example (on jsfiddle.net) is only a download is possible,but not using as source: --> <audio id="audio" preload="auto"> <source src="http://techslides.com/demos/samples/sample.mp3" type="audio/mpeg"/> <source src="http://techslides.com/demos/samples/sample.ogg" type="audio/ogg"/> <source src="http://techslides.com/demos/samples/sample.aac" type="audio/aac"/> </audio> <button id="button" disabled style="width:9em;height:3em;font-size:3em">Play</button> <br><br> <div id="log"><div><hr></div></div>
SO上的代码段不起作用,因为它位于沙箱中.
以上是大佬教程为你收集整理的javascript – IOS上的HTML5音频:currentTime如何小于初始值?全部内容,希望文章能够帮你解决javascript – IOS上的HTML5音频:currentTime如何小于初始值?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。