我正在尝试在 iOS 应用程序的 web 视图中使用 YouTube iFrame 播放器,但播放器坚持使用 iOS 播放器全屏呈现视频。我想在 webview 中播放视频。是否有一些价值我可以传递给播放器以说服它在网络视图中而不是移动 Safari 中,并且可以只在 iframe 中呈现视频?
问问题
1435 次
1 回答
2
这是 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)
于 2015-06-24T18:42:36.827 回答