Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cooperate opened this issue May 5, 2018 · 9 comments
Open

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

cooperate opened this issue May 5, 2018 · 9 comments
Assignees

Comments

@cooperate
Copy link

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
Copy link

AaronACarlson commented May 7, 2018

@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
Copy link

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

@CyanLetter
Copy link

CyanLetter commented May 7, 2018

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
Copy link
Contributor

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
Copy link

AaronACarlson commented May 8, 2018 via email

@NeilAChristensen
Copy link

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
Copy link

Thank you very much. It works perfectly!!!

@danzen
Copy link
Contributor

danzen commented May 12, 2018

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants