Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to display the remaining time of a track being played in realtime, but can't figure it out since my javascript skills are not very advanced.

Wavesurfer.js provides a function called getCurrentTime(), but I don't know how to implement it, so the time is displayed next to the play button.

Thanks for the Help!

    <!doctype html>
<meta charset="UTF-8">
<title>Unbenanntes Dokument</title>
<style type="text/css">
body {
    padding: 2em;
    padding-top:4em;
    font: "Times New Roman";
    color: white;
    background-color: black;
    letter-spacing: -.007em;
titel { line-height:1.5em; padding-bottom:13em;}
.colme {
    color: #B7B498
.subtitel {
    font-style:italic;}
#maincontent {
    float:left;
    width:700px;
    padding-right:3em;}
#sidecontent {float:left; width:300px;}
.link {font-size:1em; float:left;}
</style>
</head>
<section id="maincontent">
<titel>
<p>Lorem Ipsom<br>
</titel>
<div id="waveform"></div>
<div id="waveform-timeline"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/wavesurfer.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/plugin/wavesurfer.timeline.min.js"></script>
<script type="text/javascript"> 
    var wavesurfer = WaveSurfer.create({
    container: '#waveform',
    waveColor: 'white',
    progressColor: 'red',
    audioRate: 1,
    barWidth: 3,
    height: 250,
    pixelRatio:1
 wavesurfer.load('http://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3');
 wavesurfer.on('ready', function () {
  var timeline = Object.create(WaveSurfer.Timeline);
  timeline.init({
    wavesurfer: wavesurfer,
    container: '#waveform-timeline'
  });});
</script>
<button onClick="wavesurfer.playPause()">
    </button>
    <span style="padding-left:3em; font-family:Arial; font-size:0.8em;"> Recorder in 2016</span>
</section>
<div id="sidecontent">
<p>where</p>
<div style="clear:both"> </div>
</body>
</html>

Here is how you can do it:

I added some elements to hold the total, current and remaining time for demonstration:

<div>Total time: <span id="time-total">0.00</span> seconds</div>
<div>Current time: <span id="time-current">0.00</span> seconds</div>
<div>Ramaining time: <span id="time-remaining">0.00</span> seconds</div>

wavesurfer has an audioprocess event, which calls a function while its playing the file. I use that to get and display the times like this:

wavesurfer.on('audioprocess', function() {
  if (wavesurfer.isPlaying()) {
    var totalTime = wavesurfer.getDuration(),
        currentTime = wavesurfer.getCurrentTime(),
        remainingTime = totalTime - currentTime;
    document.getElementById('time-total').innerText = totalTime.toFixed(1);
    document.getElementById('time-current').innerText = currentTime.toFixed(1);
    document.getElementById('time-remaining').innerText = remainingTime.toFixed(1);

Here is a working example based on your code:

var wavesurfer = WaveSurfer.create({
  container: '#waveform',
  waveColor: 'white',
  progressColor: 'red',
  audioRate: 1,
  barWidth: 3,
  height: 250,
  pixelRatio:1
wavesurfer.load('https://wavesurfer-js.org/example/media/demo.wav');
wavesurfer.on('ready', function () {
  var timeline = Object.create(WaveSurfer.Timeline);
  timeline.init({
    wavesurfer: wavesurfer,
    container: '#waveform-timeline',
    primaryFontColor: '#fff',
    primaryColor: '#fff',
    secondaryColor: '#fff',
    secondaryFontColor: '#fff'
wavesurfer.on('audioprocess', function() {
  if (wavesurfer.isPlaying()) {
    var totalTime = wavesurfer.getDuration(),
        currentTime = wavesurfer.getCurrentTime(),
        remainingTime = totalTime - currentTime;
    document.getElementById('time-total').innerText = Math.round(totalTime * 1000);
    document.getElementById('time-current').innerText = Math.round(currentTime * 1000);
    document.getElementById('time-remaining').innerText = Math.round(remainingTime * 1000);
body {
  padding: 2em;
  padding-top: 4em;
  font: "Times New Roman";
  color: white;
  background-color: black;
  letter-spacing: -0.007em;
titel {
  line-height: 1.5em;
  padding-bottom: 13em;
#maincontent {
  float: left;
  width: 500px;
<script src="https://unpkg.com/wavesurfer.js"></script>
<script src="https://unpkg.com/wavesurfer.js/dist/plugin/wavesurfer.timeline.min.js"></script>
<section id="maincontent">
  <div id="waveform"></div>
  <div id="waveform-timeline"></div>
  <button onClick="wavesurfer.playPause()">Play</button>
  <div>Total time: <span id="time-total">0</span> milliseconds</div>
  <div>Current time: <span id="time-current">0</span> milliseconds</div>
  <div>Ramaining time: <span id="time-remaining">0</span> milliseconds</div>
</section>

UPDATE

For millisecods, just multiply the seconds by 1000 and round the result.

document.getElementById('time-total').innerText = Math.round(totalTime * 1000);

UPDATE 2

To change the timeline plugin colors, use the timeline plugin options. You can control 4 different colors like this:

timeline.init({
  wavesurfer: wavesurfer,
  container: '#waveform-timeline',
  primaryFontColor: '#fff',
  primaryColor: '#fff',
  secondaryFontColor: '#fff',
  secondaryColor: '#fff'
                Thank you very much Christos! I have one additional question, what would I have to do to display the remaining and current time in milliseconds and change the formatting?
– Roland
                Nov 18, 2016 at 14:57
                another question, if it's fine. I'm using the timeline plugin, but the colors aren't corresponding, since I'm using a black and not a white background. Can I only edit the colors in the js file of the timeline plugin, or is there a way to modify them without having to host a modified version myself?
– Roland
                Nov 18, 2016 at 19:42
                No need to edit the timeline plugin. It has some options to change some colors. See my updated answer for details.
– Christos Lytras
                Nov 18, 2016 at 21:16
                wavesurfer.getDuration() appears to be limited to 100th of second.  Anyway to get ACTUAL milliseconds?
– chad steele
                Feb 9 at 19:47
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.