简体   繁体   中英

p5.js sound: How to remove individual cues with removeCue()

The p5.js sound library documentation says that removeCue() can be used to cancel cued events. It says it takes an ID input that is returned from addCue().

When I invoke addCue and store the result to a variable it does not return an ID. It returns NaN.

The image below is a code example I wrote using the p5.js code editor.

How do I get the id ?

在此处输入图片说明

OK, i found the issue.

Its an issue with the library https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js

look at this link https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js#L2178

its using var id = this._cueIDCounter++; but _cueIDCounter was never defined.

so i tried to define it like the following for your code:

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

now it returned the id.

so then i tried to remove the cue with removeCue but to my surprise there is also an issue which is getting error Uncaught TypeError: Cannot read property 'splice' of undefined

so then i looked again at the library code and i realised on the following line https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js#L2198 within the removeCue function there is an error the current code is

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

but it should be using this._cues.splice(i, 1); instead of this.cues.splice(i, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM