I want to detect when sounds is ending, but all examples that i found not working.
JavaScript
x
22
22
1
// Create sound
2
var sound1 = new THREE.PositionalAudio( listener );
3
sound1.load( 'sounds/Example.ogg' );
4
sound1.setRefDistance( 30 );
5
sound1.autoplay = false;
6
sound1.setLoop(false);
7
mesh1.add( sound1 );
8
9
// Start sound
10
setTimeout(function() {
11
sound1.play();
12
}, 2000);
13
14
// Try to detect end #1
15
sound1.onended = function() {
16
console.log('sound1 ended #1');
17
};
18
// Try to detect end #1
19
sound1.addEventListener('ended', function() {
20
console.log('sound1 ended #2');
21
});
22
Live example: http://codepen.io/anon/pen/wMRoWQ
Advertisement
Answer
Three.Audio
is wrapper around a buffer source audio object. https://github.com/mrdoob/three.js/blob/ddab1fda4fd1e21babf65aa454fc0fe15bfabc33/src/audio/Audio.js#L12
It looks like it overrides the onended event and binds it to its own function, so we just do the same:
JavaScript
1
5
1
sound1.source.onended = function() {
2
console.log('sound1 ended');
3
this.isPlaying = false; /* sets Three wrapper property correctly */
4
};
5
We set isPlaying = false
because this is what Three’s Audio onended function does, which we just overrode.
working pen: http://codepen.io/anon/pen/GoambE