Wavesurfer实时获取经过时间

sun*_*yun 2 javascript html5-audio angularjs

我正在使用Katspaugh的waveSurfer库来播放声音文件。

我编写了这样的代码来显示“经过的时间/总时间”。

waveSurfer.on('play', function() {
   $scope.getCurrentDuration();
});
Run Code Online (Sandbox Code Playgroud)

$scope.getCurrentDuration() 是将浮点型变量转换为字符串型变量的函数,如下所示:

$scope.getDuration = function() {

  if ($scope.waveSurferReady) {
    var time = waveSurfer.getDuration(); // get duration(float) in second
    var minute = Math.floor(time / 60); // get minute(integer) from time
    var tmp = Math.round(time - (minute * 60)); // get second(integer) from time
    var second = (tmp < 10 ? '0' : '') + tmp; // make two-figured integer if less than 10

    return String(minute + ':' + second); // combine minute and second in string
  }

  else {
    return 'not ready yet'; // waveSurfer is not ready yet
  }
};
Run Code Online (Sandbox Code Playgroud)

但是问题在于,在这一部分中: waveSurfer.on('play', function() ...) 回调函数仅执行一次。

我希望回调函数会定期调用,但它仅执行一次,因此,经过的时间仅显示在开始时间。

我该如何解决?

rnr*_*ies 5

查看源代码,我发现该audioprocess事件与html5 timeupdate事件配对。

试试看。

waveSurfer.on('audioprocess', function() {
   // do stuff here
});
Run Code Online (Sandbox Code Playgroud)