Playing YouTube in Flutter: the case for a WebView fallback
Every Flutter app that plays YouTube picks one of two strategies, and both break. Direct extraction resolves the stream URLs and hands them to video_player . You get native playback, real quality switching and a UI you control. You also get a dependency on an interface nobody promised you: when YouTube changes something upstream, extraction returns nothing and your users get a black screen. A WebView is the strategy that doesn't break, because it's the player YouTube itself maintains. You pay for it: the chrome isn't yours, quality selection isn't yours, and integrating it with the rest of your UI is work. The useful answer is not to choose. Try extraction, and when it fails, fall back to the WebView for that video - not for the whole app, not permanently. What "fails" means Failure here isn't an exception you catch around the player widget. It's earlier: the extraction step returns no playable stream, or returns one that the platform refuses. The fallback belongs at the point where you still know which video you were resolving and haven't shown anything to the user yet. Deciding after the first black frame is too late - by then you're swapping a widget the user is already looking at. What this costs you The fallback path is not feature-equivalent, and pretending otherwise is how you get bug reports. In a WebView you lose the native quality selector and your own control overlay. So the fallback needs to be a degraded-but-working path you have actually tested, not a branch you wrote once and never exercised. Where this ended up I wrapped all of it in a package, omni_video_player : YouTube with the automatic fallback described here, plus Vimeo, HLS with quality switching, network files and assets - one widget, one controller. OmniVideoPlayer( sourceConfiguration: VideoSourceConfiguration.youtube( videoUrl: Uri.parse('https://www.youtube.com/watch?v=...'), preferredQualities: [OmniVideoQuality.high720], ), ) Two limits worth knowing before you adopt it: YouTube quality selection works on Android only, because on iOS the API exposes a single muxed 360p stream and there is nothing to switch between; and WebM seeking is off on iOS, because WebKit stalls the decoder on a seek and can't recover. Android, iOS, Web. BSD-3. https://pub.dev/packages/omni_video_player Top comments (0)
Comments
No comments yet. Start the discussion.