Skip to content

Commit dbc3202

Browse files
authored
Merge pull request aframevr#3847 from ngokevin/soundfix
fix sound component playSound on event (fixes aframevr#3844)
2 parents ebcb84a + 3de2d52 commit dbc3202

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

‎src/components/sound.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var registerComponent = require('../core/component').registerComponent;
22
var debug = require('../utils/debug');
3-
var bind = require('../utils/bind');
43
var THREE = require('../lib/three');
54

65
var warn = debug('components:sound:warn');
@@ -31,7 +30,9 @@ module.exports.Component = registerComponent('sound', {
3130
this.pool = new THREE.Group();
3231
this.loaded = false;
3332
this.mustPlay = false;
34-
this.playSound = bind(this.playSound, this);
33+
34+
// Don't pass evt because playSound takes a function as parameter.
35+
this.playSoundBound = () => { this.playSound(); };
3536
},
3637

3738
update: function (oldData) {
@@ -119,12 +120,12 @@ module.exports.Component = registerComponent('sound', {
119120
*/
120121
updateEventListener: function (oldEvt) {
121122
var el = this.el;
122-
if (oldEvt) { el.removeEventListener(oldEvt, this.playSound); }
123-
el.addEventListener(this.data.on, this.playSound);
123+
if (oldEvt) { el.removeEventListener(oldEvt, this.playSoundBound); }
124+
el.addEventListener(this.data.on, this.playSoundBound);
124125
},
125126

126127
removeEventListener: function () {
127-
this.el.removeEventListener(this.data.on, this.playSound);
128+
this.el.removeEventListener(this.data.on, this.playSoundBound);
128129
},
129130

130131
/**

‎tests/components/sound.test.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ suite('sound', function () {
186186
el.setAttribute('sound', 'src', 'url(base/tests/assets/test.ogg)');
187187
el.components.sound.isPlaying = true;
188188
});
189+
190+
test('plays sound on event', function (done) {
191+
const el = this.el;
192+
el.setAttribute('sound', 'on', 'foo');
193+
const playSoundStub = el.components.sound.playSound = sinon.stub();
194+
el.emit('foo');
195+
setTimeout(() => {
196+
assert.ok(playSoundStub.called);
197+
done();
198+
});
199+
});
189200
});
190201

191202
suite('stopSound', function () {

0 commit comments

Comments
 (0)