使用YouTube's iFrame播放器在iOS webview中渲染内联视频

Render video inline in iOS webview using YouTube's iFrame player?

本文关键字:webview 视频 iOS YouTube 播放器 iFrame 使用      更新时间:2023-09-26

我试图在iOS应用程序的webview中使用YouTube iFrame播放器,但播放器坚持使用iOS播放器全屏呈现视频。我想在网络视图中播放视频。是否有一些价值,我可以传递给播放器,以说服它,它是在一个网络视图,而不是移动Safari,它是OK的,只是在iframe内渲染视频?

这里有一个Swift的例子,如何在不强迫用户进入全屏播放器的情况下播放YouTube视频:

let webView = UIWebView(frame: CGRectMake(0, 30, self.view.frame.size.width, 500)) // or your custom rect, this is just an example
self.view.addSubview(webView)
self.view.bringSubviewToFront(webView)
// Play video in-line and start playback immediately
webView.allowsInlineMediaPlayback = true
webView.mediaPlaybackRequiresUserAction = false
// Set the id of the video you want to play
let videoID = "zN-GGeNPQEg" // https://www.youtube.com/watch?v=zN-GGeNPQEg
// Set up your player
let embededHTML = "<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width=''(self.view.frame.size.width)' height=''(self.view.frame.size.height)' src='http://www.youtube.com/embed/'(videoID)?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>"
// Load the player
webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL)