IOS10: auto play does not work on WKWebView (requires UserActionForMediaPlayback = false)

Hi, I am currently working on iOS 10 with Swift3.

My scenario is to auto-play youtube video using WKWebView. I set config mediaPlaybackRequiresUserAction to false to enable auto-play of the video, as suggested by Apple Dev Docs.

However, this configuration does not work, the video is loaded correctly, but you must press the play button to actually play it.

Any ideas? Thank:)

let config = WKWebViewConfiguration()
config.requiresUserActionForMediaPlayback = false
config.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: self.topView.bounds, configuration: config)
webView.addObserver(self, forKeyPath: self.webViewLoadingKey, options: .new, context: nil)
+4
source share
2 answers

use this for iOS10:

var mediaTypesRequiringUserActionForPlayback: WKAudiovisualMediaTypes {get set}

+5

, , - .mediaPlaybackRequiresUserAction, iOS 10.0 enter image description here

, , , WKWebView, . , H5, UIWebView

func initPureWeb(){
    let web = UIWebView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    web.backgroundColor = UIColor.clear
    web.isOpaque = false
    self.view.addSubview(web)
    web.mediaPlaybackRequiresUserAction = false
    let videoHtml = "<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'200', height:'200', videoId:'bHQqvYy5KYo', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
    web.loadHTMLString(videoHtml, baseURL: Bundle.main.resourceURL)
}
+3

Source: https://habr.com/ru/post/1655255/


All Articles