Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript wavesurfer实时获取经过的时间_Javascript_Angularjs_Html5 Audio - Fatal编程技术网

Javascript wavesurfer实时获取经过的时间

Javascript wavesurfer实时获取经过的时间,javascript,angularjs,html5-audio,Javascript,Angularjs,Html5 Audio,我用它来播放声音文件 我编写代码以这种方式显示“已用时间/总时间” waveSurfer.on('play', function() { $scope.getCurrentDuration(); }); $scope.getCurrentDuration()是一个将浮点型变量转换为字符串型变量的函数,如下所示: $scope.getDuration = function() { if ($scope.waveSurferReady) { var time = waveSur

我用它来播放声音文件

我编写代码以这种方式显示“已用时间/总时间”

waveSurfer.on('play', function() {
   $scope.getCurrentDuration();
});
$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
  }
};
$scope.getDuration=function(){
if($scope.waveSurferReady){
var time=waveSurfer.getDuration();//以秒为单位获取持续时间(float)
var minute=Math.floor(time/60);//从时间中获取分钟(整数)
var tmp=Math.round(时间-(分钟*60));//从时间中获取秒(整数)
var second=(tmp<10?'0':'')+tmp;//如果小于10,则生成两个数字整数
返回字符串(分钟+':'+秒);//在字符串中组合分钟和秒
}
否则{
返回“尚未准备就绪”;//waveSurfer尚未准备就绪
}
};
但问题是,, 在本部分:
waveSurfer.on('play',function()…)
回调函数只执行一次

我希望回调函数定期调用,但它只执行一次,因此,运行时间仅在开始时显示

如何解决这个问题?

通过查看,我发现
audioprocess
事件与html5事件成对出现

试试看

waveSurfer.on('audioprocess', function() {
   // do stuff here
});

在github页面的事件列表下方,它指出:“播放–当播放开始时。”您可能希望在“播放”事件发生时开始调用循环,可能是使用requestAnimationFrame,然后还要侦听停止事件以取消AnimationFrame谢谢!我会找出你告诉我的。还要感谢Matthias编辑了我的问题!非常感谢你!我刚刚醒来,所以我现在就要试试!