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 cur_time = myaudio.currentTime;
$('#curPosition').val(cur_time);
But it always returns 0 as current time while the audio is playing. Do anybody have any idea on this ?
Thanks
getElementsByTagName("audio")[0]; var cur_time = myaudio. currentTime; $('#curPosition'). val(cur_time); But it always returns 0 as current time while the audio is playing.
The currentTime property sets or returns the current position (in seconds) of the audio/video playback.
The HTMLMediaElement interface's currentTime property specifies the current playback time in seconds.
The duration property returns the length of the current audio/video, in seconds. If no audio/video is set, NaN (Not-a-Number) is returned.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With