[SOLVED] Sound cutting off when another event fires

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); 
    },

Please use triple: ` before and after code to make it highlighted and not messy in post.

Could you post a link to your project? What browser are you running this on?

https://playcanvas.com/project/405813/overview/introduction-to-vr

I am using chrome experemental

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.

The reason why this doesn’t work for you is because of the following:

  1. The first event fires so you play Slot 1
  2. The second event fires so you play Slot 2 but you first call sound.stop() which stops all sounds
  3. You keep firing the 2nd event again and again and again seems like every frame, so you basically call sound.stop() every frame.

You need to find out why (3) happens, it’s probably a bug in your code somewhere.

The sohbd stop is a recent change to try and figure out what was hsppening, the problem was happening before it . Removing has no effect

I have now removed all of the stop code, and the procedures and reduced the issue down to its simplest form.

its still happening.

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.

1 Like