How to play video inline with WkWebView

UBLearn picture UBLearn · May 10, 2017 · Viewed 22.1k times · Source

When you use an iphone and play video in a webview, this video is open in the native player in fullscreen.

We have tried UIWebView and WKWebView with "allowsInlineMediaPlayback" property to true. But the video in the web content launch in fullscreen with an iphone iOS 10.2. Have you and idea what i can do ?

let webConfiguration = WKWebViewConfiguration()
// Fix Fullscreen mode for video and autoplay
webConfiguration.preferences.javaScriptEnabled = true
webConfiguration.mediaPlaybackRequiresUserAction = false
webConfiguration.allowsInlineMediaPlayback = true

webView = WKWebView(frame: CGRect(x: 0, y: 0, width:self.backgroundView.frame.width, height:self.backgroundView.frame.height), configuration: webConfiguration)

Env : Xcode 8, swift 3

Answer

onewh picture onewh · May 16, 2018

There is no problem for your code,but you need one more step, the video URL you use should always with a parameter playsinline=1.

//step1
if let videoURL:URL = URL(string: "https://somevideo.mp4?playsinline=1")
//step2
webConfiguration.allowsInlineMediaPlayback = true

then you can do the left things.