使用YouTube的iFrame播放器在iOS网页视图中内嵌视频?

时间:2013-04-04 05:34:16

标签: javascript ios html5 webview youtube

我正在尝试在iOS应用中的webview中使用YouTube iFrame播放器,但播放器坚持使用iOS播放器将视频全屏渲染。我想在webview中播放视频。是否有一些价值我可以传递给玩家以说服它在网页视图中而不是移动版Safari,只需在iframe中呈现视频就可以了?

1 个答案:

答案 0 :(得分:0)

以下是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)
相关问题