Skip to content

Instantly share code, notes, and snippets.

@silvb
Last active July 30, 2026 09:21
Show Gist options
  • Select an option

  • Save silvb/5a7504828b4e84afec867d9c7bbe1568 to your computer and use it in GitHub Desktop.

Select an option

Save silvb/5a7504828b4e84afec867d9c7bbe1568 to your computer and use it in GitHub Desktop.
Loopy Demos: iOS silent mode and embedded demos (why cross-origin iframes cannot set the audio session, and the snippet that fixes it)

Loopy Demos: iOS silent mode and embedded demos

On iPhone and iPad, audio from an embedded Loopy Demos widget is silenced by the physical ring/silent switch unless the embedding page opts in. This page explains why, and gives you the snippet that fixes it.

If your product page already plays audio fine on desktop and Android, this is the only mobile-specific thing you need to add.

The symptom

A visitor on an iPhone with the silent switch engaged:

  • opens your product page
  • presses play on the Loopy Demos widget
  • sees the widget playing, but hears nothing
  • has to flip the physical switch off to hear the demo

Most visitors will not do that. They will assume the demo is broken.

Why it happens

iOS assigns every page an audio session category. The default category is auto, which resolves to ambient: audio respects the ring/silent switch, the same as a game's background music. The category that ignores the switch is playback, which is what a music or video app uses.

Safari 16.4 and later expose this to the web through the Audio Session API:

navigator.audioSession.type = "playback"

The Loopy Demos widget already does this. It works when a demo is viewed directly on loopydemos.com, and it works when the widget is embedded on a page from the same origin.

It does not work when the widget is embedded on your site, because WebKit refuses this assignment from a cross-origin iframe. The audio session belongs to the top-level page, and a third-party frame is not allowed to escalate it. The write is silently ignored: no exception, no console warning, the value simply stays at auto.

This restriction is deliberate and reasonable. Without it, any advertising iframe could push audio through a silenced phone.

Measured behaviour

Reading navigator.audioSession.type in each context on iOS 26, silent switch on:

Context audioSession.type Audible on silent
loopydemos.com, top level playback yes
iframe, same origin as parent playback yes
iframe on your site, cross-origin auto (write rejected) no

Things that do not fix it

These were each tested directly and made no difference. Listed so you do not spend time on them:

  • allow="autoplay" on the iframe. Permissions Policy governs autoplay, not the audio session category.
  • Setting the category inside a user gesture. The write is rejected in a cross-origin frame with or without an active user gesture.
  • Preloading, or otherwise reducing the delay between the tap and the first audio sample. This is not a timing or network-speed issue.

The fix

The audio session belongs to your page, so your page has to set it. The current Loopy Demos snippet is a placeholder div plus one script tag, and the script builds the iframe for you:

<div
  data-loopydemos="orange-or30"
  data-title="Orange OR30 | Loopy Demos"
  data-width="600"
  data-height="697"
></div>

<script src="https://loopydemos.com/embed.js" async></script>

That is the whole integration. The script is under two kilobytes over the wire, with no dependencies and no build step, and the script tag can go anywhere on the page.

The embed configurator on each demo page generates this snippet with your colors and dimensions filled in, so you normally do not have to write it by hand.

Because the script is what creates the frame, this form requires it: if it is blocked or fails to load, nothing renders. If that is a risk you cannot take, see Already have an iframe? below, or the inline fallback at the end.

What it does

The widget broadcasts its state to the parent page through the postMessage API (see the embedding and postMessage documentation). The script listens for those events and does two things:

  1. Audio session. Sets your page's category to playback while a demo is playing, and back to auto when it stops.
  2. Automatic height. The widget reports its real content height, and the script sizes the iframe to match. This fixes the empty space below the widget on narrow viewports, keeps it correct when the visitor rotates their phone, and follows preset switches that show a differently sized pedal.

The second one is worth having even if you do not care about iOS. An iframe cannot size itself to its content, and a cross-origin parent cannot measure it, so the height in your embed code is a fixed guess made when you copied the snippet. This replaces the guess with a measurement.

Browsers other than Safari do not implement navigator.audioSession, so the audio half is a no-op there while the height half still applies. It is safe to ship to all visitors.

Notes on the details

The category applies to your whole page, not just the widget. While it is set to playback, any other audio or video on the same page also stops respecting the silent switch. This is why the script sets it only while a demo is actually playing and restores auto afterwards. If you have several demos on one page, it is released only once they have all stopped.

Message origins are validated. The script only acts on messages from https://loopydemos.com, so other framed content on your page cannot use it to change your audio behaviour.

If you use a tag manager, load the script on DOM ready or earlier. It also watches for embeds added to the page later, so injected or lazily rendered iframes are picked up without extra work.

Content Security Policy. If your site sends one, allow script-src https://loopydemos.com. You will already have frame-src for the iframe itself.

Options

Attribute Default Purpose
data-loopydemos required The demo slug
data-title Loopy Demos The iframe's accessible title
data-width 600 Maximum width in pixels. The frame is fluid below it
data-height 600 Starting height, replaced as soon as the widget reports its own

Theming

Either set the colors as attributes:

<div
  data-loopydemos="orange-or30"
  data-primary="#f57e20"
  data-secondary="#272727"
  data-text="#f57e20"
  data-background="#272727"
  data-primary-highlight="#ffffff"
  data-secondary-highlight="#ffffff"
  data-tertiary-highlight="#000000"
></div>

Or drive them from your stylesheet with custom properties, which is usually nicer if you already have brand tokens:

.product-demo {
  --loopydemos-embed-primary: #f57e20;
  --loopydemos-embed-secondary: #272727;
  --loopydemos-embed-text: #f57e20;
  --loopydemos-embed-background: #272727;
  --loopydemos-embed-primary-highlight: #ffffff;
  --loopydemos-embed-secondary-highlight: #ffffff;
  --loopydemos-embed-tertiary-highlight: #000000;
}
<div class="product-demo" data-loopydemos="orange-or30"></div>

Custom properties win where both are present, so you can ship defaults in the markup and override them in CSS.

Three things to know:

  • Hex only. Values are passed to the widget as hex colors. rgb(), named colors and gradients are ignored rather than forwarded as something that would render incorrectly.
  • The prefix is --loopydemos-embed-, deliberately different from the widget's internal --loopydemos- tokens, so the two cannot collide on a page that happens to define both.
  • Read once, at mount. The colors are applied when the frame is created. Toggling a dark mode afterwards will not re-theme an already-rendered widget.

Beyond the seven colors above, success, error, orange, subdued and black are also themeable, in both forms.

Already have an iframe?

Then you do not have to change your markup. The script also enhances a Loopy Demos iframe that your page owns, so adding the script tag next to your existing embed is enough:

<div style="display: flex; justify-content: center;">
  <iframe
    loading="lazy"
    src="https://loopydemos.com/demos/orange-or30/embed"
    height="697"
    width="600"
    title="Orange OR30 | Loopy Demos"
    style="border-radius: 12px; border: 0px;"
  ></iframe>
</div>

<script src="https://loopydemos.com/embed.js" async></script>

This is also the more resilient of the two: if the script is blocked or fails to load, the iframe carries on working exactly as it does today, just without the two enhancements. The trade-off is a fixed height in your templates and more markup to maintain, which is why the placeholder form is the one handed out now. Both are supported and neither is going away.

If you cannot add a third-party script

Some security policies forbid loading scripts from external domains. In that case, keep your iframe and inline this instead. It covers the audio fix only, not the automatic height:

<script>
  window.addEventListener("message", function (event) {
    if (event.origin !== "https://loopydemos.com") return
    if (!navigator.audioSession) return
    if (!event.data || typeof event.data.type !== "string") return

    if (event.data.type === "loopydemos:play") {
      navigator.audioSession.type = "playback"
    }

    if (event.data.type === "loopydemos:pause") {
      navigator.audioSession.type = "auto"
    }
  })
</script>

The origin check is required. Without it, any other framed content on the page could change your audio behaviour. Do not remove it, and do not replace it with a check on event.data alone.

Verifying it

You need a real iOS device. The iOS Simulator and desktop Safari's responsive design mode do not model the silent switch.

  1. Engage the physical ring/silent switch on the iPhone.
  2. Fully quit Safari from the app switcher. The audio session category persists across page loads within a Safari session, so a stale session will give you a false pass.
  3. Open your product page and press play on the widget.
  4. You should hear audio.

To inspect it directly, connect the iPhone to a Mac and use Safari's Web Inspector (Develop menu, then your device, then the page). In the console, on the top-level page context rather than the iframe:

navigator.audioSession.type

Before play it should read auto, and during playback playback.

To confirm the script itself loaded, on the same top-level context:

window.loopydemosEmbed

That returns a version and origin once the script is running.

Still on the plain iframe?

Nothing breaks, and there is no deadline. Embeds without the script keep working as they always have. They log a one-off notice to the browser console pointing here, so that a developer looking at the page knows the option exists.

Questions

Mail silvio@loopydemos.com.

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