feat(macos): background audio/media playback for WKWebView#368
feat(macos): background audio/media playback for WKWebView#368COCPORN wants to merge 3 commits intoblackboardsh:mainfrom
Conversation
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>
|
A quick google looks like we probably shouldn't use those private APIs? e.g. ionic-team/cordova-plugin-ionic-webview#286 |
|
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. |
|
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 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. |
|
@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! |
Summary
mediaPlaybackRequiresUserAction = NO,allowsInlineMediaPlayback = YES)backgroundMediaas a JS-configurable option on BrowserWindow (defaults tofalse, no behavior change for existing apps)Test plan
backgroundMedia: false(default) preserves existing behavior🤖 Generated with Claude Code