如何使用wavesurfer.js实时显示剩余时间

8
我正在尝试实时显示正在播放的曲目的剩余时间,但由于我的JavaScript技能不太高级,无法解决问题。
Wavesurfer.js提供了一个名为getCurrentTime()的函数,但我不知道如何实现它,以便将时间显示在播放按钮旁边。
谢谢帮助!
    <!doctype html>
<html>
<head>
<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>

<body>


<section id="maincontent">
<titel>
<p>Lorem Ipsom<br>
</p>
</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()">
      Play
    </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>

<div style="clear:both"> </div>
</body>
</html>

2
获取总时间,然后减去当前时间。 - Feathercrown
1个回答

19

以下是如何操作:

我添加了一些元素来展示总时间当前时间剩余时间

<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有一个audioprocess事件,当播放文件时会调用一个函数。我使用它来获取并显示时间,就像这样:

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);
  }
});

这是一个基于您的代码的工作示例:

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>

更新

对于毫秒级别,只需将秒数乘以1000并四舍五入结果即可。

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

更新 2

要更改时间线插件的颜色,请使用时间线插件选项。您可以像这样控制4种不同的颜色:

timeline.init({
  wavesurfer: wavesurfer,
  container: '#waveform-timeline',
  primaryFontColor: '#fff',
  primaryColor: '#fff',
  secondaryFontColor: '#fff',
  secondaryColor: '#fff'
});

非常感谢你,Christos!我还有一个问题,如果我想要显示剩余时间和当前时间(以毫秒为单位),并更改格式,我需要做什么? - Roland
1
很容易。请查看我的更新答案并运行更新的代码。 - Christos Lytras
还有一个问题,如果可以的话。我正在使用时间轴插件,但是颜色不对应,因为我使用的是黑色而不是白色背景。我只能编辑时间轴插件的js文件中的颜色,还是有一种方法可以修改它们,而无需自己托管修改后的版本? - Roland
不需要编辑时间轴插件。它有一些选项可以更改一些颜色。有关详细信息,请参见我的更新答案。 - Christos Lytras
wavesurfer.getDuration() 似乎被限制在百分之一秒。有没有办法获取实际毫秒数? - chad steele

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接