Accessors support timed and periodic events with the setTimeout() and setInterval() functions. Each takes a callback function and milliseconds as arguments. Any further arguments will be passed to the callback function.
Here, we'll make a clip player that plays the first seconds of a sound clip.
setup() creates an instance of the ClipPlayer accessor. addInputHandler() starts the player upon a start event and stops it 15000 milliseconds later by sending a value to ClipPlayer's stop input. setInterval() is used to execute a function at a certain time in the future.
Click 'react to inputs'. The clip should play for approximately 15 seconds. The clip may take a few seconds to load and start.
Next, add a parameter to control the clip duration.
/** An accessor that plays a sound clip for the specified duration.
* @accessor tutorial/TimedClipPlayer.
* @input start Start playback.
*/
exports.setup = function() {
this.input('start', {'value' : true});
this.player = this.instantiate('ClipPlayer', 'audio/ClipPlayer');
this.connect('start', this.player, 'start');
};
exports.initialize = function() {
var self = this;
// Upon start, stop the player after 15000 milliseconds.
this.addInputHandler('start', function() {
setInterval(function() {
self.player.send('stop', true);
}, 15000);
});
};
Modules required: util