Skip to content

feat(macos): background audio/media playback for WKWebView#368

Closed
COCPORN wants to merge 3 commits intoblackboardsh:mainfrom
COCPORN:feat/macos-background-audio
Closed

feat(macos): background audio/media playback for WKWebView#368
COCPORN wants to merge 3 commits intoblackboardsh:mainfrom
COCPORN:feat/macos-background-audio

Conversation

@COCPORN
Copy link
Copy Markdown

@COCPORN COCPORN commented Apr 8, 2026

Summary

  • Enables background audio/media playback in WKWebView (mediaPlaybackRequiresUserAction = NO, allowsInlineMediaPlayback = YES)
  • Prevents macOS from suspending the content process when the app window is occluded — without this, audio stops when switching away from the app
  • Exposes backgroundMedia as a JS-configurable option on BrowserWindow (defaults to false, no behavior change for existing apps)

Test plan

  • Verified audio continues playing when app window is behind other windows
  • Verified audio continues when switching to another desktop/space
  • Verified backgroundMedia: false (default) preserves existing behavior
  • Running in production for ~2 weeks

🤖 Generated with Claude Code

ungedenstad and others added 3 commits April 8, 2026 20:44
WKWebView suspends media playback when the application loses focus.
This adds three configuration properties to prevent that:

- mediaTypesRequiringUserActionForPlayback = None (public API)
- _alwaysRunsAtForegroundPriority = YES (keeps content process active)
- _mediaDataLoadsAutomatically = YES (eager media loading)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The initial foreground priority settings weren't enough — WebKit still
suspended media when the window was 2+ Spaces away due to macOS window
occlusion detection.

Two additions fix this:
- Disable _pageVisibilityBasedProcessSuppressionEnabled on WKPreferences
  so WebKit never throttles the content process based on visibility
- Override occlusionState on ElectrobunWindow to always report visible

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add backgroundMedia boolean to BrowserView and BrowserWindow options
(defaults false) so background audio/media playback is opt-in rather
than unconditionally enabled for all webviews.

- Gate WKWebView configuration flags behind backgroundMedia check
- Make ElectrobunWindow occlusion state override conditional
- Pass backgroundMedia through FFI via setNextWebviewFlags
- Add flag plumbing to Linux/Windows native wrappers (unused stubs)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@chrisvariety
Copy link
Copy Markdown

A quick google looks like we probably shouldn't use those private APIs? e.g. ionic-team/cordova-plugin-ionic-webview#286

@COCPORN
Copy link
Copy Markdown
Author

COCPORN commented Apr 10, 2026

Sorry, this is sloppy on my behalf. You bring up a good point. This is (obviously) Claude and I authoring this together, I am not an expert on macOS internals.

Feel free to close this with "won't merge", I'll maintain my own fork of this until I figure out how to do this cleanly in a different way. I personally don't need to be on the AppStore.

What this does is allow for notification sounds to play when long-running tasks complete in Electrobun app, I found that the app would very quickly suspend when navigating away from the workspace the Electrobun-app is running on and upon navigating back to the app there would be a stampede of notification-sounds played.

I'm researching alternative routes to make this solid, but it is not high priority for me as in the short term I will just be using this hack, but I might be back with a PR that is better researched and implemented.

@COCPORN
Copy link
Copy Markdown
Author

COCPORN commented Apr 10, 2026

Closing this — we've moved to CEF, so the WKWebView-specific private API workarounds here are no longer needed. If CEF exhibits a similar suspension issue, it'll need a different approach (Chromium switches / CefSettings). Thanks for the review feedback @chrisvariety!

@COCPORN COCPORN closed this Apr 10, 2026
@chrisvariety
Copy link
Copy Markdown

@COCPORN awesome, if you land on a CEF solution definitely let me know! I only clicked on this because I will need a similar "please don't suspend me" solution soon, not for audio playing but for a similar long-running background process.

@COCPORN
Copy link
Copy Markdown
Author

COCPORN commented Apr 11, 2026

@chrisvariety At first glance, it looks like this issue simply doesn't surface with CEF — Chromium doesn't seem to do the same occlusion-based suspension that WKWebView does. We haven't done deep testing on it yet, but background audio and processes have been working fine for us without any workarounds. Might be worth just trying your use case on CEF before investing in a solution!

@COCPORN COCPORN deleted the feat/macos-background-audio branch April 12, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants