I’ve been engaged on a private iOS mission for enjoyable — basically 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 off — I bought actually curious. I’ve been making an attempt to duplicate that fundamental background audio performance for YouTube embeds utilizing WKWebView. I’ve spent a loopy period of time (most likely 20 hours) making an attempt to determine this out however have achieved no success.
Right here’s what I’ve tried thus far:
-Embedding a YouTube video in a WKWebView
-Activating AVAudioSession with .playback and setting .setActive(true)
-Including the UIBackgroundModes key with audio in Data.plist
-Including the NSAppTransportSecurity key to permit arbitrary masses
–Testing on an actual machine (iPhone 14, iOS 18.1 goal)–
What occurs:
Audio performs advantageous within the foreground.
If I exit the app and go to the lock display shortly sufficient (lower than 3 seconds) after urgent play, I can resume playback briefly from the lock display — however it doesn’t mechanically 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 constantly within the logs:
Error buying assertion:
It looks like 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 totally different beneath the hood? A customized participant? A SafariViewController trick? Is there a selected technique to configure WKWebView to maintain taking part in within the background, or is that this a recognized limitation?
Would actually admire any perception from people who’ve explored this earlier than or understand how apps like Musi pulled it off.
Thanks prematurely!
Enhance