admin管理员组文章数量:1391937
I'm working on a plain JavaScript + HTML audio player and at this time I want to build a interface that show the current time of the audio in the form "MM:SS".
For what I could search the HTML Audio/Video DOM Reference has only one native function - audio|video.currentTime
- that return the current audio time in seconds with a large decimal precision.
In order to do this I code:
HTML
<audio id='audio' src=".mp3" controls></audio>
<div id="timer">00:00</div>
JavaScript
var audio = document.getElementById('audio'),
timer = document.getElementById('timer');
var update = setInterval(function() {
timer.innerHTML = audio.currentTime;
}, 10);
I tried a bunch of thing but I could not format the output the way I wanted. Hope some of you guys can bring some light on this.
Codepen sample
I'm working on a plain JavaScript + HTML audio player and at this time I want to build a interface that show the current time of the audio in the form "MM:SS".
For what I could search the HTML Audio/Video DOM Reference has only one native function - audio|video.currentTime
- that return the current audio time in seconds with a large decimal precision.
In order to do this I code:
HTML
<audio id='audio' src="http://www.stephaniequinn./Music/Canon.mp3" controls></audio>
<div id="timer">00:00</div>
JavaScript
var audio = document.getElementById('audio'),
timer = document.getElementById('timer');
var update = setInterval(function() {
timer.innerHTML = audio.currentTime;
}, 10);
I tried a bunch of thing but I could not format the output the way I wanted. Hope some of you guys can bring some light on this.
Codepen sample
Share Improve this question asked Apr 20, 2016 at 19:29 João Paulo SaragossaJoão Paulo Saragossa 3133 silver badges18 bronze badges2 Answers
Reset to default 7Working edited CodePen
Just needed a little math! JS below:
var update = setInterval(function() {
var mins = Math.floor(audio.currentTime / 60);
var secs = Math.floor(audio.currentTime % 60);
if (secs < 10) {
secs = '0' + String(secs);
}
timer.innerHTML = mins + ':' + secs;
}, 10);
I think this is the most elegent way:
var seconds = audio.currentTime % 60;
var minutes = Math.floor(audio.currentTime / 60);
timer.innerHTML = ('0' + minutes).substr(-2) + ':' + ('0' + seconds).substr(-2);
本文标签: Parsing and showing HTML audio current time with javascriptStack Overflow
版权声明:本文标题:Parsing and showing HTML audio current time with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744686408a2619726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论