Hi,
I have an entity called Narator which has various sounds in slots and it has a script attached that listens for events (see below) . The problem is that as soon as the second scene “scene2” fires the sound that was playing previously is cut off despite it being in a different slot to the one that is told to play.
Narator.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
app.on('scene1', function () {
this.entity.sound.play('Slot 1');
}, this);
app.on('scene2', function () {
this.entity.sound.play('Slot 2');
}, this);
app.on('scene3', function () {
this.entity.sound.play('Slot 3');
}, this);
app.on('scene4', function () {
this.entity.sound.play('Slot 4');
}, this);
app.on('scene5', function () {
this.entity.sound.play('Slot 5');
}, this);
app.on('scene6', function () {
this.entity.sound.play('Slot 6');
}, this);
},
just getting frustrated now, this should just work, i don’t know why the audio has to be so hard. it’s one line of markup in aframe and in playcanvas the simplest stuff just doesn’t work.
Here are the two bits of code, as you can see it very simple.
here is the first script which fires two events, each event only fires once
pc.script.create('director', function (app) {
// Creates a new Director instance
var Director = function (entity) {
this.entity = entity;
this.globaltime =0;
this.scene=0;
};
Director.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
this.globaltime += dt;
//console.log(this.globaltime);
if (this.globaltime>1 && this.scene!=1) {
this.scene =1;
app.fire('scene1');
}
if (this.globaltime>10 && this.scene!=2) {
this.scene =2;
app.fire('scene2');
}
}
};
return Director;
});
and here is the other which listens for the events
pc.script.create('Narator', function (app) {
// Creates a new Narator instance
var Narator = function (entity) {
this.entity = entity;
};
Narator.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
app.on('scene1', function () {
this.entity.sound.play('Slot 1');
}, this);
app.on('scene2', function () {
this.entity.sound.play('Slot 2');
}, this);
app.on('scene3', function () {
this.entity.sound.play('Slot 3');
}, this);
app.on('scene4', function () {
this.entity.sound.play('Slot 4');
}, this);
app.on('scene5', function () {
this.entity.sound.play('Slot 5');
}, this);
app.on('scene6', function () {
this.entity.sound.play('Slot 6');
}, this);
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
}
};
return Narator;
});
the firing of scene2 stops the sound in slot 1 but does not start the sound in slot 2
I used the debugger and the issue got even stranger
putting the debugger statement in before it plays the second sound allows the first sound to play all the way through.
putting the debugger statement in after it plays the second sound allows both the first and the second sound to play together.
So now I am at a complete loss to know why the sound is being shut off and why simply halting execution after playing the second sound is allowing the code to work. I don’t have any code anywhere which is stopping audio.
found out what the issues was in the end. I was swapping between sounds back and forward with every update so neither got chance to play. thank you for your post it spurred me on to find the root of the problem.