pixijs / sound

WebAudio API playback library, with filters. Modern audio playback for modern browsers.
https://pixijs.io/sound/examples/
MIT License
399 stars 68 forks source link

Chrome 66 - The AudioContext was not allowed to start #65

Open otoinsa opened 6 years ago

otoinsa commented 6 years ago

Hi! First of all - thanks for this great sound library! :)

Since last Chrome update there's a new policy: https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio

Needless to say, I don't like this new policy.

image

My game audio context gets paused :( Since the game has no mouse intereaction, it never gets resumed.

However if I do press on the canvas with a pointer device, it resumes the audio context. PIXI.sound.resumeAll() does not help either.

Is there no way to work around this?

I really like this library and I've been using it for my game; It's still in active development and pixi-sound is an important part of it.

bigtimebuddy commented 6 years ago

Thanks for the compliments.

I don’t think you can avoid this. We are heading to a world where all audio and video media requires an interaction. I’m not a huge fan either. You can design around it by creating a start button?

otoinsa commented 6 years ago

It's a good idea, but one of the target devices (arcade box) will only have button controls connected to it (directional joystick, 5 action buttons), currently a keypress is not resuming the audio context. :(

I guess I can build with an older version of Chromium for now until the Chrome devs implement some none pointer-device way or resuming audio context...

bigtimebuddy commented 6 years ago

I wonder if there is there a flag in Chrome to prevent this behavior?

otoinsa commented 6 years ago

chrome://flags/ does have an "Autoplay policy" flag actually, so far it works. :)

bigtimebuddy commented 6 years ago

Follow-up article: https://www.bleepingcomputer.com/news/google/google-fixes-issue-that-broke-millions-of-web-based-games-in-chrome/

Looks like they temporarily reverting the change until more developers can integrated interaction-to-play audio. I might add a note about this in the readme because it's an important design consideration.

OptimusPi commented 6 years ago

Sorry to bump an old post--

I added a "start" button that then plays the main menu state and starts the audio. Yet, I still get this warning:

The Web Audio autoplay policy will be re-enabled in Chrome 70 (October 2018). Please check that your website is compatible with it. https://goo.gl/7K7WLu

Is that warning something that always pops up? OR is it telling me my current implementation simply doesn't work correctly? Thanks!

OptimusPi commented 5 years ago

How do we resume the audio context after user interaction?

chipbell4 commented 5 years ago

Based on this setter @OptimusPi I believe you should be able to set paused = false, ala

PIXI.sound.context.paused = false;
useless-stuff commented 4 years ago

maybe it could be useful for someone, it took me a while sorting out those annoying warnings.

You can load dynamically 'pixi-sound' after a user click/swipe and store it in a lazy loader var.

if (!this.pixiSound) {
  this.pixiSound = await import('pixi-sound')
}

then, in order to use it:

  this.pixiSound.default
connorjclark commented 3 years ago

Building off the previous comment, this work well for me:

document.addEventListener('click', async () => {
  globalThis.PIXI.sound = (await import('pixi-sound')).default;
}, { once: true });