Skip to content

Detect sound is ended in THREE.PositionalAudio?

I want to detect when sounds is ending, but all examples that i found not working.

// Create sound
var sound1 = new THREE.PositionalAudio( listener );
sound1.load( 'sounds/Example.ogg' );
sound1.setRefDistance( 30 );
sound1.autoplay = false;
mesh1.add( sound1 );
// Start sound
setTimeout(function() {;
}, 2000);
// Try to detect end #1
sound1.onended = function() {
    console.log('sound1 ended #1');
// Try to detect end #1
sound1.addEventListener('ended', function() {
    console.log('sound1 ended #2');

Live example:


Three.Audio is wrapper around a buffer source audio object.

It looks like it overrides the onended event and binds it to its own function, so we just do the same:

sound1.source.onended = function() {
    console.log('sound1 ended');
    this.isPlaying = false; /* sets Three wrapper property correctly */

We set isPlaying = false because this is what Three’s Audio onended function does, which we just overrode.

working pen: