niconiconi / Unblock iOS Background WebAudio

Published:

Version: 1+006b5ce updated

Summary: This script intercepts Web Audio contexts and shows a pop-up window to enable autoplay, allowing you to play Web games on iOS with sound that are otherwise incompatible. Background audio autoplay is disallowed on iOS, users are not even allowed to add exceptions. Thus, many existing Web apps, games, WebAssembly apps in particular, are incompatible with iOS without modifications.

License: 0BSD

This script intercepts Web Audio contexts and shows a pop-up window to enable autoplay, allowing you to play Web games on iOS with sound that are otherwise incompatible. Background audio autoplay is disallowed on iOS, users are not even allowed to add exceptions. Thus, many existing Web apps, games, WebAssembly apps in particular, are incompatible with iOS without modifications.

Note: by default, this script matches ALL websites, since the iOS "Userscript" extension has no way to apply scripts to user-defined websites. Thus, make sure to enable this script only when it's needed, and disable it otherwise.

For more information, please check the blog post Force Enable Web Audio Autoplay on iOS for Incompatible Apps via Userscript.

Demo

The following three emulators had no audio due to aforementioned compatibility problem,
which is successfully fixed by my Userscript.

DOSBox-X / PC-98

PC-98 emulation by a WebAssembly port of DOSBox-X via Emscripten, by yksoft1,
running the game Touhou 4: 東方幻想郷 ~ Lotus Land Story

Pinky / NES

NES emulation by Pinky, an NES emulator written in Rust, running the game Super Mario Bros..

PCjs / IBM PC

IBM PC emulation by PCjs, a JavaScript emulator of IBM PC and compatibles, running the game Nine Princes in Amber

Limitations

This Userscript doesn't always work due to several limitations.

First, even with @run-at document-start, technical limitations on iOS make it
impossible to run Userscript exactly before everything else. If the Web pages
creates an AudioContext at an very-early moment (e.g. in a JavaScript file
that runs as soon as the page is loaded), this AudioContext cannot be
intercepted.

Next, background audio can be implemented via methods other than Web Audio's
AudioContext. This script makes no attempt to patch them.

Installation

  1. Install "Userscript" app written
    by Justin Wasack (quoid) from App Store.
    Note that this is free software licensed under GPLv3, you can obtain its source code from
    GitHub.
Install Userscript from App Store
  1. Open "Userscript" app from iOS home screen, click "Set Userscript Directory", create
    and select an empty directory via iOS's file manager for storing Userscripts. Do not skip
    this step, otherwise the App won't be functional.
Open App from home screen Set a directory via iOS's file manager
  1. Use Safari to open my script Unblock iOS Background WebAudio hosted on OpenUserJS, click "Install". Now the source code of the script should appear on the
    screen. You may use this chance review the code to see how it works.

  2. Click the "puzzle" icon at the right of the address bar to open
    the list of browser extensions, click "Userscript", and grant permissions.

Click Userscript in the list of browser extensions Grant permissions
  1. Now the Userscript menu should open, with a "Userscript Detected: Tap to Install"
    notice. Tap the notice to install.
Tap to install
  1. The script metadata should appear in the menu, scroll the menu to the end,
    and click "Install".
Script information appears Scroll to the end and click Install
  1. For every new website that you need to run Userscript, repeat step 4 to
    grant permissions, then refresh the page. Furthermore, you can check whether
    a Userscript is active by noticing whether its name is highlighted in the menu.

Note: By default, this script matches ALL websites, since the iOS "Userscript" extension has no way to apply scripts to user-defined websites. Thus, make sure to enable this script only when it's needed, and disable it otherwise.

Rating: 0