p5.j​​s声音:如何使用removeCue()删除单个提示

时间:2018-06-18 02:53:44

标签: javascript web-audio p5.js

p5.js声音库文档说removeCue()可用于取消提示事件。它说它需要从addCue()返回的ID输入。

当我调用addCue并将结果存储到变量时,它不会返回ID。它返回NaN。

下面的图片是我使用p5.js代码编辑器编写的代码示例。

我如何获得身份证?

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题。

它是库https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js

的问题

查看此链接https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js#L2178

它使用var id = this._cueIDCounter++;,但从未定义_cueIDCounter

所以我尝试为您的代码定义如下:

Object.defineProperty(mySound,'_cueIDCounter',{value:1,writable:true});

现在它返回了ID。

因此,我尝试用removeCue删除提示,但令我惊讶的是,还有一个问题出现了错误Uncaught TypeError: Cannot read property 'splice' of undefined

因此,我再次查看了库代码,并意识到在removeCue函数的下一行https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js#L2198中,当前代码存在错误

p5.SoundFile.prototype.removeCue = function (id) {
    var cueLength = this._cues.length;
    for (var i = 0; i < cueLength; i++) {
      var cue = this._cues[i];
      if (cue.id === id) {
        this.cues.splice(i, 1);
      }
    }
    if (this._cues.length === 0) {
    }
};

但是它应该使用this._cues.splice(i, 1);而不是this.cues.splice(i, 1);