A kind reminder we would like all registered users to weigh in on one of our forum's security policies.
Please take a moment to read this thread and place a vote.
https://forum.palemoon.org/viewtopic.php?f=17&t=32935

Questions about AudioContext behavior

Users and developers helping users with generic and technical Pale Moon issues on all operating systems.

Moderator: trava90

Forum rules
This board is for technical/general usage questions and troubleshooting for the Pale Moon browser only.
Technical issues and questions not related to the Pale Moon browser should be posted in other boards!
Please keep off-topic and general discussion out of this board, thank you!
User avatar
UCyborg
Astronaut
Astronaut
Posts: 605
Joined: 2019-01-10, 09:37
Location: Slovenia

Questions about AudioContext behavior

Post by UCyborg » 2025-07-12, 00:57

1. Some interactions with the web browser cause the audio context to arbitrarily suspend and resume itself. Why is that? For instance, you click the icon to the left of URL bar, then More information... button and the context is suspended.

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>
Now this small test suggests if I open More information..., the context remains suspended indefinitely if I don't do anything. But the real app I'm playing with, when it receives the event to react to, checks if context is in running state and only proceeds to play sound if it is. And it does play the sound, even though this test suggests it shouldn't.

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.

User avatar
UCyborg
Astronaut
Astronaut
Posts: 605
Joined: 2019-01-10, 09:37
Location: Slovenia

Re: Questions about AudioContext behavior

Post by UCyborg » 2025-07-12, 14:17

So basically the only reason to complicate code is if Pale Moon user happens to be opening More information..., settings etc.

The specs say AudioContext starts in suspended state, definitely true for Mozilla based, Chrome is a bit vague, well apparently you can still expect onstatechange event handler to fire at the beginning and their own blog says you can normally check the state of AudioContext right away after creating it to determine if sound is expected to play without requiring user interaction, which is not something that's expected to work on Mozilla based, though you can use onstatechange change event handler. For the thing I'm messing, I'd like to have a checkbox to toggle sound on and off and determine its default state based on the initial AudioContext state.

User avatar
UCyborg
Astronaut
Astronaut
Posts: 605
Joined: 2019-01-10, 09:37
Location: Slovenia

Re: Questions about AudioContext behavior

Post by UCyborg » 2025-07-12, 14:54

Before I get further lost with testing too many combinations, here are my observations when it comes Pale Moon:
  1. Point 1 in OP.
  2. If Pale Moon is started while Windows audio services are off, AudioContext stays unusable indefinitely, even if services come back and media playback in the browser work. AudioContext stays in suspended state and calling resume() on it results in a Promise that stays in pending state.
  3. media.autoplay.enabled pref doesn't cover AudioContext and no separate setting exists (to my knowledge) for audio contexts.

User avatar
UCyborg
Astronaut
Astronaut
Posts: 605
Joined: 2019-01-10, 09:37
Location: Slovenia

Re: Questions about AudioContext behavior

Post by UCyborg » 2025-07-13, 20:42

I figured AudioContext is not usable while preferences window is open. Nothing can update it seems (modal dialog). Closing preferences window makes the AudioContext resume on its own.

But why is AudioContext suspended when opening Page info, which is a non-modal dialog? Worse, this doesn't even happen consistently all the time! It happens on freshly run instance, but later it may not. Actually, when it decided to not occur, it did not occur in that simple HTML, but it still did in the app I'm playing with.

Does anyone even care or am I talking to a wall?

And then I'm the ignorant and narrow minded one when I say programs (and programmers) haven't changed a bit in the last 25 years.

I haven't ruled out though that extensions aren't changing behavior long-term. Usually, they aren't.

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 38534
Joined: 2011-08-28, 17:27
Location: Sweden

Re: Questions about AudioContext behavior

Post by Moonchild » 2025-07-13, 21:12

It's possible that the Page Info dialog suspends the audio context because it enumerates media used in the page (including audio).
Your test file does reflect the audiocontext state in its checkbox so the only thing that seems to be unexpected is that it gets suspended by the dialog being opened.
UCyborg wrote:
2025-07-13, 20:42
Does anyone even care or am I talking to a wall?
For something this specific? I think you need a bit more patience. I've seen your posts but I'm not an expert in this area of the code to begin with so can't really help beyond semi-educated guesses as I've done here. And 4 posts spread over a weekend without an immediate reply doesn't mean you're being ignored.
What you likely do achieve by being impatient and abrasive about "no response yet after a day, bah!" is that people are less inclined to pay attention to your posts. But eh, what do I know, right? I could be wrong.
"There is no point in arguing with an idiot, because then you're both idiots." - Anonymous
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
UCyborg
Astronaut
Astronaut
Posts: 605
Joined: 2019-01-10, 09:37
Location: Slovenia

Re: Questions about AudioContext behavior

Post by UCyborg » 2025-07-13, 21:47

My bad. I'm in a ranty mood lately, things are getting to me.

This is probably one of those messy things inherited by Mozilla.

Kris_88
Board Warrior
Board Warrior
Posts: 1168
Joined: 2021-01-26, 11:18

Re: Questions about AudioContext behavior

Post by Kris_88 » 2025-07-13, 21:56

It is interesting that opening a new window (file -> new window) also stops playback in the window in which the specified menu item is clicked. But other browser windows, if any, are not affected. Only the window in which we clicked "file -> new window" is affected.
Unfortunately, I don't have time to dig into this deeply. Maybe later...

Edit:
Oh, it's even more interesting there. If two windows are open and the example under discussion is open in each, then opening a third window does not affect the original two.
In general, strange behavior. Obviously some kind of bug...