Skip to content

Instantly share code, notes, and snippets.

@surma
Last active February 23, 2022 10:35
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save surma/301c9c377aaf90a3fdae615d4840bb2e to your computer and use it in GitHub Desktop.
Save surma/301c9c377aaf90a3fdae615d4840bb2e to your computer and use it in GitHub Desktop.
/**
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
const list = [];
self.AudioContext = new Proxy(self.AudioContext, {
construct(target, args) {
const result = new target(...args);
list.push(result);
return result;
}
});
const btn = document.createElement('button');
btn.classList.add('unmute');
btn.style.position = 'fixed';
btn.style.bottom = '0';
btn.style.right = '0';
btn.textContent = '🔇 Unmute';
btn.style.fontSize = '5em';
btn.onclick = e => {
list.forEach(ctx => ctx.resume());
btn.remove();
};
document.addEventListener('DOMContentLoaded', _ => {
document.body.appendChild(btn);
});
})();
@hoch
Copy link

hoch commented May 8, 2018

NOTE: this code snippet displays a button that resumes blocked AudioContexts by the autoplay policy. For more detail on the autoplay change in Chrome, see https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio.

@andrevenancio
Copy link

Hey @surma I find it interesting that a dev within Google is adding a fix for an issue that was created by Google :)
Wouldn't a request from the browser itself be better? Something like we have with the notifications / location / webcam. Its better than forcing the user to click a button which might do more than just authorise a AudioContext..

@cab77
Copy link

cab77 commented May 30, 2018

Doesn't work for mobile Chrome, Chrome 66.0.3359.158 on Android breaks HTML5 audio on my website, reverting back to 64.0.3282.137 works. Interestingly I'm not autoplaying anything... Sound is only triggered when user clicks a play button. So not sure whether this is completely related or another unrelated 'feature' added by Google.

And they wonder why web development is a dogs breakfast having to accommodate numerous different browsers instead of there being a single standard that all browsers adhere to...

Desktop Version 66.0.3359.181 currently works, but I've just received an update notification so lets see if it manages to break the audio...

@Eiion
Copy link

Eiion commented Dec 7, 2018

Doesn't work for Chrome v72

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment