检测声音在THREE.PositionalAudio中结束? [英] Detect sound is ended in THREE.PositionalAudio?

查看:71
本文介绍了检测声音在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;
sound1.setLoop(false);
mesh1.add( sound1 );

// Start sound
setTimeout(function() {
    sound1.play();
}, 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');
});

实例 http://codepen.io/anon/pen/wMRoWQ

推荐答案

Three.Audio 是缓冲区源音频对象的包装器。 https://github.com/mrdoob/three .js / blob / ddab1fda4fd1e21babf65aa454fc0fe15bfabc33 / src / audio / Audio.js#L12

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:

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

我们设置 isPlaying = false 因为这是 Three's Audio onended功能,我们只是过度了。

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

工作笔: http://codepen.io / anon / pen / GoambE

这篇关于检测声音在THREE.PositionalAudio中结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆