-
Notifications
You must be signed in to change notification settings - Fork 2
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
Timers are unavailable until AudioContexts are allowed to run. #462
Comments
As far as I can tell the autoplay policy gets lifted in any browser as soon as you resume any https://stackblitz.com/edit/js-vvbsjc?file=index.js,index.html Anyway, it feels not okay to create an import { setContext, setTimeout } from 'audio-context-timers';
const audioContext = new AudioContext();
setContext(audioContext);
document.onclick = () => {
audioContext.resume();
setTimeout(() => console.log('yeah'), 1000);
} |
@chrisguttandin Thanks. Yes a More info about what we were trying to do:
To service // timers.ts
import { setTimeout as actSetTimeout } from 'audio-context-timers';
export const createTimer = (callback: Function, delay): TimerId => {
if (canUseActTimers) { // <----- how to know this?
return actSetTimeout(callback, delay);
}
return window.setTimeout(callback, delay);
}; To answer this question we made a local AudioContext just for the purpose of attempting to A
Alternatively, if a import { setTimeout as actSetTimeout, getContext as actGetContext } from 'audio-context-timers';
export const createTimer = (callback: Function, delay): TimerId => {
if (actGetContext().state === 'running') {
return actSetTimeout(callback, delay);
}
return window.setTimeout(callback, delay);
}; Or if we definitely wanted to use our own AudioContext for timers to avoid browser limits, etc, we could use both import {
setTimeout as actSetTimeout,
setContext as actSetContext,
getContext as actGetContext
} from 'audio-context-timers';
import { getGlobalAudioContext } from '../local-audio-service';
export const createTimer = (callback: Function, delay): TimerId => {
let timerContext = actGetContext();
const globalContext = getGlobalAudioContext();
if (!timerContext && globalContext) {
actSetContext(globalContext);
timerContext = globalContext;
}
if (timerContext && context.state === 'running') {
return actSetTimeout(callback, delay);
}
return window.setTimeout(callback, delay);
}; So anyway I guess I'd advocate for both a |
Thanks for telling me more about your use case. It got me thinking. Would it be an option for you to use worker-timers instead? |
Per autoplay policy, an AudioContext won't be allowed to run until a user gesture of some sort happens. Calls to
audio-context-timers.setTimeout
made before this happens won't function as expected.One solution: expose the audio context used to provide the timer service. Clients can then observe that context to see when it should be safe to use its timers.
Another solution might be to allow clients to provide the AudioContext. This would also allow clients to better manage the number of AudioContexts running around and avoid possibly bumping up against browser limits.
The text was updated successfully, but these errors were encountered: