如何在webviewFrame中播放视频?

时间:2015-10-26 08:31:33

标签: ios iphone uiwebview

我正在尝试在UIWebView框架内播放视频

我想在我的webview框架中播放视频,我看到有很多选项,比如嵌入视频和所有在UIWebview.But我希望它像一个播放器,像youtube播放器视频将在webview中播放而不显示全屏。

提前致谢

1 个答案:

答案 0 :(得分:0)

您的视频是否在YouTube上托管?

let webView = UIWebView(frame: self.view.frame) // Position your player frame here

self.view.addSubview(webView)
self.view.bringSubviewToFront(webView)

// Playback options: play in-line and start playing 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 HTML into your UIWebView
webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL)

或者,YouTube为开发者提供了一个很棒的helper class,可以在他们的iOS中设置YouTube播放,为您设置UIWebView