Skip to content Skip to sidebar Skip to footer

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);

DEMO

Solution 2:

you can try like this

<audioid="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><spanid="tracktime">0 / 0</span><buttononclick="document.getElementById('track').play();">Play</button>

or in javascript you can do this

<audioid='audioTrack'ontimeupdate='updateTrackTime(this);'>
  Audio tag not supported in this browser</audio><script>functionupdateTrackTime(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"