When I open the dev tools, the context is resumed.
I've put together small test HTML with a checkbox that reflects audio context state:
Code: Select all
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AudioContext test</title>
<script type="text/javascript">
'use strict';
var audioContext;
function onAudioContextStateChange() {
document.querySelector('#audswitch').checked = audioContext.state === 'running';
}
window.addEventListener('load', () => {
audioContext = new AudioContext({
latencyHint: 'playback'
});
console.log('Initial state: ' + audioContext.state);
audioContext.onstatechange = onAudioContextStateChange;
document.querySelector('#audswitch').addEventListener('click', function() {
if (this.checked) {
audioContext.resume().then(() => console.log('On'));
} else {
audioContext.suspend().then(() => console.log('Off'));
}
return false;
});
});
</script>
</head>
<body>
<h2>AudioContext test</h2>
<input id="audswitch" type="checkbox">Enable audio</input>
</body>
</html>
Not sure what I'm missing here. This behavior may be confusing when debugging something. I can't consistently reproduce, but something else happened to me few times when nothing was happening on the page, repeatedly checking audio context state in console returned different results, that was after it was suspended by the code.
2. Chromium and Firefox's autoplay settings cover AudioContext in addition to audio and video elements. Currently, only audio and video elements are covered in Pale Moon, but pages can still play audio through AudioContext without user interaction. An area for improvement?
Edit: Regarding 1., re-tested something, got it wrong the first time... The point stands, why interacting with the web browser messes with the audio context?? I guess you could respond appropriately in onstatechange handler, but why?
Chromium is the only one where I get the running state right away after creating AudioContext unless autoplay settings block it, not the case on Mozilla based. But at least Firefox leaves it alone once it goes into running state sometimes later after creation.
Edit 2: OK, I think I found a way to rework the code for these quirks to not be bothersome.


