☑ No audio playing

Despite having the following:
An Entity with a enabled AudioListener AND
An Entity with non positional Sound with a clip

When running script… doesn’t play any sounds
I created a brand new sound object with auto-play and a loaded slot… and nothing gets played

I figure this must be some configuration issue … or maybe leftover remains from previous projects…

var AudioManager = pc.createScript(‘audioManager’);
AudioManager.attributes.add(‘soundMeow’, { type: ‘asset’, assetType: ‘audio’, array: false });

////////////////////////////////////////////////////////////////////
self.soundChannelMeow = self.soundChannels[iChannelIndex]; // array of entities with sound objects

var soundMeow = self.soundMeow.resource;

var soundSlotMeow = self.soundChannelMeow.sound.addSlot( ‘Meow’,
{ asset: soundMeow,
pitch: 1.7,
loop: false,
autoPlay: false,
overlap:true
} );
self.soundChannelMeow.sound.play(‘Meow’);

        self.soundChannelMeow.sound.play('Meow');

http://playcanvas.com/editor/scene/456028

You need to pass the asset to the addSlot function, not asset.resource.

Still no audio… Sounds with autoplay are not played

Your audioManager.js script is disabled.

I did it on purpose… the entity with audiolistener is active and the sound with autoplay is active also… so audiomanager is not needed

I hear the bongo play once just fine. But then, I’m using Chrome which can play OGG files. You might be using a browser that can’t. MP3 is a much better format for playing in just about any browser.

You are right… apparently FIREFOX have issues playing OGG files… I managed to play the sound on the mobile browser… Thanks playcanvas… Will try mp3 to make sure it works on all platforms

2 Likes

I have been trying to make a script that plays a sound when space key is pressed but it wont work I need help
this is what i have:
annonMove.prototype.update = function(dt) {
if( this.app.keyboard.isPressed(pc.input.KEY_RIGHT)){
this.entity.translateLocal(0, 0, -0.3);
}
if( this.app.keyboard.isPressed(pc.input.KEY_LEFT)){
this.entity.translateLocal(0, 0, 0.3);
}

if ( this.app.keyboard.isPressed(pc.KEY_SPACE)){
this.entity.sound.play(‘Sound’);

} else{
this.entity.sound.stop(‘Sound’);
}
};

Can you share the project please?

https://playcanvas.com/project/551202 this is it. When i run the project it says “Cannot read property ‘play’ of undefined”

You are trying to access a sound component on the cannon where there is none, hence the error.

Have a look on some of the tutorials and project samples for audio here: https://developer.playcanvas.com/en/tutorials/?tags=audio

I added a sound component and everything but i just cant play audio from a script!

You are trying to play sound ‘Boom’ but your sound slot name is still ‘Slot 1’.

Now i am not getting an error when i press space but no audio is playing.

You need to remove the boolean. As it is, every frame the game checks to see if the button is pressed, and because of how frequently that is your boolean playing gets set true, and then false instantly, so no sound. if(app.keyboard.isPressed(pc.KEY_SPACE)) is enough on it’s own to trigger the sound event. If you have a problem with the sound continuing to play after the key has been released, just turn off loop.

EDIT: I stand corrected, I’m not getting sound when I test this.

On Chrome there’s a google policy change and a warning in console that says “The AudioContext was not allowed to start. It must be resume (or created) after a user gesture on the page. https://goo.gl/7K7WLu

I am not sure I understand.

What this means is that Google Chrome has done something that I think is fairly silly. My understanding is that they’ve broken the audio for every HTML5 game until the developers of said game add extra code to re-enable the audio based on a user gesture event.

While this is the case, I’ve simply moved to using and recommending Firefox to all users of my game

Maybe the PlayCanvas devs can implement a workaround for Chrome’s arbitrary restrictions in the engine itself, but until then we have to handle it ourselves.

Oh okay now i get it

My audio has been working now that I have been using Microsoft edge

Try adding this code in the initialize function of one of your scripts:

    var app = this.app;
    var resumeContext = function () {
        app.systems.sound.manager.context.resume();            
        window.removeEventListener('mousedown', resumeContext);
        window.removeEventListener('touchend', resumeContext);
    };
    
    window.addEventListener('mousedown', resumeContext);
    window.addEventListener('touchend', resumeContext);

This should fix the Chrome policy change.

1 Like