在使用audio标签的时候,有时候我们的需求是使某首旋律从指定的时间开始播放,
那么我们就用到了audio标签的currentTime属性,
其中使用这个标签的h5页面在微信浏览器中都能够正常播放,但是被嵌套在app里后,只有安卓机能够正常赋值从而在指定的时间开始播放,Ios机不能够正常赋值
So....看我如何解决,直接上代码~~~~~
首先判断机型
1 isAndroid: function() { 2 const u = navigator.userAgent; 3 if (u.indexOf("Android") > -1 || u.indexOf("Linux") > -1) { 4 return true; 5 } 6 }
然后就根据不同的机型,书写不同的代码
1 upperPagecurtime () { 2 const that = this; 3 const oAudio = document.getElementById("audio"); 4 if (that.isAndroid()) { 5 oAudio.currentTime = para.musicCurtime; // 安卓兼容写法 6 } else { 7 oAudio.addEventListener("canplay", function() { 8 oAudio.currentTime = para.musicCurtime; // ios兼容写法 9 }); 10 } 11 }
至此~~以上代码便能够兼容IOS手机,从而使audio标签在苹果app中也能够实现从指定的时间播放音乐的功能啦