I would like to display the current time of of an html 5 audio element and the duration of that element. I've been looking over the internet but can't find a functional script which allows me to display how long the audio files is and what the time currently is e.g. 1:35 / 3:20.
Any one got any ideas :)?
Definition and Usage The currentTime property sets or returns the current position (in seconds) of the audio/video playback.
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.
How to embed audio in HTML? To embed audio in HTML, we use the <audio> tag. Before HTML5, audio cannot be added to web pages in the Internet Explorer era. To play audio, we used web plugins like Flash.
HTML Audio - How It WorksThe controls attribute adds audio controls, like play, pause, and volume. The <source> element allows you to specify alternative audio files which the browser may choose from. The browser will use the first recognized format.
Here's an example:
<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>
This should work in Firefox and Chrome, for other browsers you'll probably need to add alternative encodings.
here is simple usage. keep in mind html5 elements are still in the works so anything can change:
audio = document.getElementsByTagName("audio")[0]; //functions audio.load(); audio.play(); audio.pause(); //properties audio.currentSrc audio.currentTime audio.duration
here is the reference HTML5 Audio
to get the currentTime while audio is playing you must attach the timeupdate
event and update your current time within the callback function.
simple tutorial audio/video html5 on dev.opera
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