Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 448
Akhil Sundar Avatar asked Mar 19 '14 09:03

Akhil Sundar


People also ask

How do I get the current time from a sound tag?

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.

What is audio currentTime?

The currentTime property sets or returns the current position (in seconds) of the audio/video playback.

What does currentTime do in Javascript?

The HTMLMediaElement interface's currentTime property specifies the current playback time in seconds.

How do I find the length of a video in HTML?

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.


1 Answers

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

like image 81
laaposto Avatar answered Nov 15 '22 04:11

laaposto