【问题标题】:AudioContext .ended not firing properlyAudioContext .ended 没有正确触发
【发布时间】:2017-09-25 11:05:27
【问题描述】:

我有一个简单的例程,它创建一个音频上下文节点并使用缓冲区加载它。 onended 事件似乎在一开始就触发了。我为持续时间设置了一个计时器,并且 onended 事件在音频停止播放之前触发了大约该时间。任何人都知道如何获得一个真正的onended事件。我尝试使用目标和源,结果相同

function playSoundFile(buffer) {
   'use strict';
   audioCtx.close();
   audioCtx = new (window.AudioContext || window.webkitAudioContext)();
   audioSource = audioCtx.createBufferSource();
   audioSource.connect(audioCtx.destination);
   audioSource.buffer = buffer;
   audioSource.playbackRate.value = 1;
   stillPlaying(true);
   audio_player = audioCtx.destination;
   audioSource.addEventListener('onended', whatsUp());
   console.log('start ' + showTime());
   filePlayed(buffer.duration);
   audioSource.start();
}

播放和结束事件之间的时间戳仅相差 1 毫秒。 filePlayed 启动超时事件以显示开始和结束的时间。

【问题讨论】:

  • 注意,问题中的代码会立即调用whatsUp(),而不是引用函数来调用事件。
  • audioSource.addEventListener('onended', whatsUp); 替换audioSource.addEventListener('onended', whatsUp());

标签: javascript audiocontext


【解决方案1】:

好的,guest271314 基本上是对的。

文档令人困惑。 eventListener 是“结束”而不是“结束”,您必须使用带有参数的函数名称

所以

audioSource.addEventListener('ended', whatsup);

解决问题。谢谢。

【讨论】:

  • 这对我也有用。请接受您自己的正确答案,以作为其他人知道的方式。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 2020-03-31
  • 2019-02-28
  • 2020-03-07
  • 1970-01-01
  • 2015-07-09
  • 2018-07-30
相关资源
最近更新 更多