CreateJS / SoundJS

A Javascript library for working with Audio. It provides a consistent API for loading and playing audio on different browsers and devices. Currently supports WebAudio, HTML5 Audio, Cordova / PhoneGap, and a Flash fallback.
http://createjs.com/
MIT License
4.42k stars 838 forks source link

Is SoundJS effected by recent changes to Chrome autoplay? #297

Open cooperate opened 6 years ago

cooperate commented 6 years ago

Hi I'm using SoundJS in my application but recent changes to Chrome have caused it to stop working.

https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

What should I do to fix this?

AaronACarlson commented 6 years ago

@cooperate I'm having the same problem. I'm getting, "The AudioContext was not allowed to start." Does anyone have any fixes for this?

loalexzzzz commented 6 years ago

@cooperate I'm having the same problem as well, what should I do for this?

CyanLetter commented 6 years ago

Fix seems to be to resume the audio context manually with a click handler. I ran across someone having the same issue with a different audio library:

http://www.html5gamedevs.com/topic/37384-no-sound-or-music-on-chrome-desktop-windows-10/?tab=comments#comment-213612

Based on the solution posted there, this seems to be working for me in SoundJS. You should be able to throw this in pretty much anywhere, I added it during the initialization phase of a game.

var resumeAudioContext = function() {
    // handler for fixing suspended audio context in Chrome
    try {
        if (createjs.WebAudioPlugin.context && createjs.WebAudioPlugin.context.state === "suspended") {
            createjs.WebAudioPlugin.context.resume();
            // Should only need to fire once
            window.removeEventListener("click", resumeAudioContext);
        }
    } catch (e) {
        // SoundJS context or web audio plugin may not exist
        console.error("There was an error while trying to resume the SoundJS Web Audio context...");
        console.error(e);
    }
};
window.addEventListener("click", resumeAudioContext);

EDIT: Updated code based on danzen's comment below - Should no longer throw errors if WebAudioPlugin hasn't been instantiated

gSkinner-Blair commented 6 years ago

Noted, thanks for bringing this to our attention. We're already doing similar unlocks on iOS, so we'll look into generalizing these for other browsers and contexts.

AaronACarlson commented 6 years ago

That is working for me. Thanks much!

-Aaron

On Mon, May 7, 2018 at 10:47 AM, Dakota Ling notifications@github.com wrote:

Fix seems to be to resume the audio context manually with a click handler. I ran across someone having the same issue with a different audio library:

http://www.html5gamedevs.com/topic/37384-no-sound-or-music- on-chrome-desktop-windows-10/?tab=comments#comment-213612

Based on the solution posted there, this seems to be working for me in SoundJS. You should be able to throw this in pretty much anywhere, I added it during the initialization phase of a game.

var resumeAudioContext = function() { // handler for fixing suspended audio context in Chrome try { if (createjs.WebAudioPlugin.context.state === "suspended") { createjs.WebAudioPlugin.context.resume(); // Should only need to fire once window.removeEventListener("click", resumeAudioContext); } } catch (e) { // SoundJS context or web audio plugin may not exist console.error("There was an error while trying to resume the SoundJS Web Audio context..."); console.error(e); } }; window.addEventListener("click", resumeAudioContext);

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/CreateJS/SoundJS/issues/297#issuecomment-387108882, or mute the thread https://github.com/notifications/unsubscribe-auth/AZqhMZIyePUA5Xvoe7Au6_o2WaM0W-q1ks5twGyCgaJpZM4TzeHB .

NeilAChristensen commented 6 years ago

CyanLetter saves the day! Thanks for sharing the fix.

FYI, all the sound demos on the SoundJS site are also suffering from the same issue on initial load. Once you click to view a new demo it rights itself due to the use of iFrames (I'm guessing).

This is how I confirmed that the error I was seeing was not only in my code.

sandracabello commented 6 years ago

Thank you very much. It works perfectly!!!

danzen commented 6 years ago

Thanks CyanLetter - the code worked on a server but I found, when testing locally, it still gives errors in chrome and stops the code from running in Firefox. I have adjusted the code to at least fix Firefox locally. So much for the try catch - I don't think I have EVER gotten a try catch to work in 30 years of coding.

var resumeAudioContext = function() {
    // handler for fixing suspended audio context in Chrome 
    try {
        if (createjs.WebAudioPlugin.context && createjs.WebAudioPlugin.context.state === "suspended") {
            createjs.WebAudioPlugin.context.resume();
        }
    } catch (e) {
        // SoundJS context or web audio plugin may not exist
        console.error("There was an error while trying to resume the SoundJS Web Audio context...");
        console.error(e);
    }
    // Should only need to fire once
    window.removeEventListener("click", resumeAudioContext);
};
window.addEventListener("click", resumeAudioContext);
CyanLetter commented 6 years ago

Update for anyone keeping track of this, Google has updated Chrome 66 to temporarily remove the new autoplay policy for the Web Audio API. It appears that everything using SoundJS is working properly again.

However, they are planning to add this feature back in Chrome 70 in October, so this is only a temporary fix.

Here is the relevant post in the Chrome bug tracker: https://bugs.chromium.org/p/chromium/issues/detail?id=840866#c103