The following is a brief discussion of h5 custom audio (problems and solutions). The content is quite good, so I will share it with you now and give it as a reference.
h5 activity needs to insert audio, but also needs to customize the style, so I write it myself
html
<!-- cur表示当前时间 max表示总时长 input表示进度条 --> <span class='cur'></span><input type="range" min=0 max=100 class='range' value=0><span class='max'></span>
css
#/* 进度条 */ .range { width: 5.875rem; height: 0.15rem; background: #2386e4; border-radius: 0.25rem; -webkit-appearance: none !important; position: absolute; top: 3.55rem; left: 6rem; } /* 进度滑块 */ .range::-webkit-slider-thumb { width: 0.5rem; height: 0.5rem; background: #fff; border: 1px solid #f18900; cursor: pointer; border-radius: 0.25rem; -webkit-appearance: none !important; }
js
//将秒数转为00:00格式 function timeToStr(time) { var m = 0, s = 0, _m = '00', _s = '00'; time = Math.floor(time % 3600); m = Math.floor(time / 60); s = Math.floor(time % 60); _s = s < 10 ? '0' + s : s + ''; _m = m < 10 ? '0' + m : m + ''; return _m + ":" + _s; } //触发播放事件 $('.play').on('click',function(){ var audio=document.getElementById('ao'); audio.play(); setInterval(function(){ var t=parseInt(audio.currentTime); $(".range").attr({'max':751}); $('.max').html(timeToStr(751)); $(".range").val(t); $('.cur').text(timeToStr(t)); },1000); }); //监听滑块,可以拖动 $(".range").on('change',function(){ document.getElementById('ao').currentTime=this.value;$(".range").val(this.value); });
The above can basically achieve custom audio playback, but in There was a problem when dragging the progress bar. It was ok on the computer, but it could be dragged on the mobile phone. However, the total duration of the audio was several minutes shorter than normal playback, resulting in inaccurate playback after dragging the progress bar. Through testing, it was found that the duration (total duration) obtained on the mobile phone was different from that on the computer, resulting in inaccurate playback position after sliding. I found out that because the uploaded audio was compressed by me, the duration I got on my phone was different from the normal one. Therefore, after the audio is compressed, its duration will change on the mobile phone (not on the computer), so you should pay attention to it in the future. If there is any way to compress the audio and get the normal duration on the mobile phone, please let me know, haha.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
H5 horizontal and vertical screen detection method
The above is the detailed content of Analysis of h5 custom audio. For more information, please follow other related articles on the PHP Chinese website!