I’ve been engaged on a private iOS challenge for enjoyable — primarily a YouTube music participant, studying how background media playback works in native iOS apps.
After seeing that Musi (a well-known music streaming app) can play YouTube audio within the background with the display screen off — I bought actually curious. I’ve been making an attempt to duplicate that primary background audio performance for YouTube embeds utilizing WKWebView. I’ve spent a loopy period of time (in all probability 20 hours) making an attempt to determine this out however have achieved no success.
Right here’s what I’ve tried to date:
-Embedding a YouTube video in a WKWebView
-Activating AVAudioSession with .playback and setting .setActive(true)
-Including the UIBackgroundModes key with audio in Information.plist
-Including the NSAppTransportSecurity key to permit arbitrary hundreds
–Testing on an actual machine (iPhone 14, iOS 18.1 goal)–
What occurs:
Audio performs high quality within the foreground.
If I exit the app and go to the lock display screen rapidly sufficient (lower than 3 seconds) after urgent play, I can resume playback briefly from the lock display screen — but it surely doesn’t robotically proceed like in Musi and different apps prefer it.
More often than not, the audio stops when the app is backgrounded.
I get this error persistently within the logs:
Error buying assertion:
It looks as if the app lacks some particular entitlements associated to WebKit media playback. I don’t have AppDelegate/SceneDelegate (utilizing SwiftUI), however can add if wanted.
I’m tremendous curious how music streaming apps utilizing youtube as a supply get round this — are they doing one thing completely different beneath the hood? A customized participant? A SafariViewController trick? Is there a particular strategy to configure WKWebView to maintain taking part in within the background, or is that this a recognized limitation?
Would actually recognize any perception from people who’ve explored this earlier than or understand how apps like Musi pulled it off.
Thanks upfront!
Enhance