[SOLVED] Sound Event Listener on Play not working

Hello Team!

Without any input the SoundComponents often don’t start even if AutoPlay is activated. I would like to track when the sound starts. Looking up an Entity and accessing the Sound Element seems to work but the on-Play event never fires…

    var soundOrigin =  this.app.root.findByName('SoundOrigin').sound;
    console.log("hasevent"+soundOrigin.hasEvent('play')); //<-- false
    
    soundOrigin.on('play', function (a, b) {
    console.log('Sound started');
});

SoundOrigin is an entity with a Sound and track component. I think I have a logic error…

If you need my Editor setup just tell me.

Thanks for the Help && CHEERS :smiley:

On which browsers do you have this problem? Some browsers do not allow sound to be played before the user has given an input.

Thanks for the reply @Albertos!

I am aware of this restriction and actually I really welcome it. I hate autoplay sound on sites! Using chrome here. The problem is not the sound not playing directly. But when the sound plays (because of an interaction) I want to know. Thats what the listener on the event is for but it will not connect.

Ah okay, then I misunderstood your problem. Sorry!

If you have a solution for the problem I mentioned, (in safari on mobile in my case), let me know!

2 Likes

In real life a user interaction would have already occured rendering this particular problem obsolete.
But having an event working would be desirable. I would not like to hack this via script-click and I dont even know if this would work.

I will keep testing :sunny:

1 Like

Hi @Sebastian,

Checking the source code of the sound component/slot it seems that a play event will definitely fire on the component:

If it doesn’t it’s either a bug or the sound starts playing before you register your on play callback. I can’t think of anything else.

I’d say try submitting a bug issue on the engine repo about it.

1 Like

In addition to the above, please also supply a project that reproduces the error :slight_smile:

2 Likes

Yes, the sound play event will get fired.

I worked on creating a simple testcase for you to look at and realised a couple of things:

  • Even if Autoplay is turned to FALSE the browser (chrome) throws a warning because of the created AudioContext. This is okay but confused me - my fault.

  • Setting Autoplay to TRUE will not trigger the onPlayEvent when the user interacts for the first time. The onResumeEvent will also not work.

Demo:
https://playcanvas.com/project/680856

The Soundboggs-Entity has the sound emitter.
The Avatar has a visualizer script which trys to register the play event.
Once you click into the scene the sound starts but the Event never fires.

If you set the Soundboggs-Sound to autoplay=false and activate the onMousedown function in the visualizer script you get the working solution.

1 Like

Guys, this is kinda a fringe case and it doesn’t cause me any problems. The event works everytime after the first user interaction. You can flag it solved. I would like to know if you consider this a bug or standard behavior or ‘this is just the way the web works’. Just out of curiosity.

Having a quick look at the engine code, it feels like a bug as even on autoPlay, the event should still be fired :thinking:

Can you please add a ticket to the engine repo? The example project you provided is great too, thanks!

2 Likes

Filed an issue: https://github.com/playcanvas/engine/issues/2017#issue-606618438

Marking this as solved since my initial Problem is resolved :blush:

Thanks @yaustar @Leonidas @Albertos

3 Likes