在iOS应用中嵌入YouTube视频,而不是全屏播放。

时间:2021-04-12 18:58:56

I am trying to play YouTube videos in an app through the UIWebView. I realize that this is quite easy, but the video will automatically play fullscreen. Is there a way to play it not fullscreen, and furthermore, for the video size to change when the UIWebView's size is changed? I am looking for an answer in Swift.

我试图通过UIWebView在一个应用程序中播放YouTube视频。我知道这很简单,但是视频会自动播放全屏。是否有一种方法可以让它不全屏播放,而且,当UIWebView的大小改变时,视频的大小也会改变?我正在迅速寻找答案。

I found a possible answer at this answer, but it was written in Objective- C and I found it quite difficult to translate.

我发现了一个可能的答案,这个答案,但这是写在Objective - C,我发现它很难翻译。

1 个解决方案

#1


0  

To answer your first question, every time the UIWebView changes size, the iFrame player will reload. You can't change that.

要回答第一个问题,每次UIWebView改变大小时,iFrame播放器都会重新加载。你不能改变这一点。

As for setting up, you're going to want to dynamically get the width and height of the device at runtime, and pass them into your UIWebView and player constructors. This will play the video "inLine," but full screen. Pass in whatever width and height values you want into the constructors to allow inLine playback without the video popping open to its full screen player.

至于设置,您将希望在运行时动态获取设备的宽度和高度,并将它们传递到您的UIWebView和播放器构造函数中。这将播放“内联”的视频,但是全屏。传入任何宽度和高度值,您希望在构造函数中允许内联重放,而不需要对其全屏播放器打开视频。

let webView = UIWebView(frame: self.view.frame)

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

webView.allowsInlineMediaPlayback = true
webView.mediaPlaybackRequiresUserAction = false

let videoID = "zN-GGeNPQEg" // https://www.youtube.com/watch?v=zN-GGeNPQEg

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>"

webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().resourceURL)

Result:

结果:

在iOS应用中嵌入YouTube视频,而不是全屏播放。

#1


0  

To answer your first question, every time the UIWebView changes size, the iFrame player will reload. You can't change that.

要回答第一个问题,每次UIWebView改变大小时,iFrame播放器都会重新加载。你不能改变这一点。

As for setting up, you're going to want to dynamically get the width and height of the device at runtime, and pass them into your UIWebView and player constructors. This will play the video "inLine," but full screen. Pass in whatever width and height values you want into the constructors to allow inLine playback without the video popping open to its full screen player.

至于设置,您将希望在运行时动态获取设备的宽度和高度,并将它们传递到您的UIWebView和播放器构造函数中。这将播放“内联”的视频,但是全屏。传入任何宽度和高度值,您希望在构造函数中允许内联重放,而不需要对其全屏播放器打开视频。

let webView = UIWebView(frame: self.view.frame)

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

webView.allowsInlineMediaPlayback = true
webView.mediaPlaybackRequiresUserAction = false

let videoID = "zN-GGeNPQEg" // https://www.youtube.com/watch?v=zN-GGeNPQEg

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>"

webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().resourceURL)

Result:

结果:

在iOS应用中嵌入YouTube视频,而不是全屏播放。