Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 display audio currentTime

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 :)?

like image 344
Lenny Magico Avatar asked Feb 14 '11 14:02

Lenny Magico


People also ask

What is audio currentTime?

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

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.

How do I embed audio in HTML5?

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.

How do I show audio controls in HTML?

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.


2 Answers

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.

like image 161
robertc Avatar answered Sep 24 '22 20:09

robertc


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

like image 42
KJYe.Name Avatar answered Sep 23 '22 20:09

KJYe.Name