Javascript/HTML5: Get Current Time Of Audio Tag
I have an audio tag in my template and I need to show currentTime of it on a button click. Please check my code below: var myaudio = document.getElementsByTagName('audio')[0]; var
Solution 1:
It is a typo. You declare var myaudio
and then you use audio.currentTime
not myaudio.currentTime
Try:
var myaudio = document.getElementsByTagName("audio")[0];
var cur_time = myaudio.currentTime;//<-----Change here
$('#curPosition').val(cur_time);
Solution 2:
you can try like this
<audio id="track" src="http://upload.wikimedia.org/wikipedia/commons/a/a9/Tromboon-sample.ogg"
ontimeupdate="document.getElementById('tracktime').innerHTML = Math.floor(this.currentTime) + ' / ' + Math.floor(this.duration);">
<p>Your browser does not support the audio element</p>
</audio>
<span id="tracktime">0 / 0</span>
<button onclick="document.getElementById('track').play();">Play</button>
or in javascript you can do this
<audio id='audioTrack' ontimeupdate='updateTrackTime(this);'>
Audio tag not supported in this browser</audio>
<script>
function updateTrackTime(track){
var currTimeDiv = document.getElementById('currentTime');
var durationDiv = document.getElementById('duration');
var currTime = Math.floor(track.currentTime).toString();
var duration = Math.floor(track.duration).toString();
currTimeDiv.innerHTML = formatSecondsAsTime(currTime);
if (isNaN(duration)){
durationDiv.innerHTML = '00:00';
}
else{
durationDiv.innerHTML = formatSecondsAsTime(duration);
}
}
</script>
I am currently getting time out of javascript
Post a Comment for "Javascript/HTML5: Get Current Time Of Audio Tag"