在UIWebView中未定义Youtube iFrame API pauseVideo [英] Youtube iFrame API pauseVideo is undefined in UIWebView
问题描述
我正在为iPad开发一个应用程序,其中包含一个或多个嵌入在UIWebView中的YouTube视频。我正在使用Youtube iframe API。视频显示正常,用户可以正常使用正常控件。但是,我想在外部事件发生时暂停视频。
I am developing an app for iPad which contains one or more youtube videos embedded in a UIWebView. I am using the Youtube iframe API. The video shows up fine and the user can use the normal controls fine. However, I want to pause the video when an external event happens.
我试图通过将stringByEvaluatingJavascripFromString与player.pauseVideo()一起使用来实现这一点。但是,在检查时看来player.pauseVideo是未定义的。
I am attempting to do this by using stringByEvaluatingJavascripFromString with "player.pauseVideo()". However, on inspection it appears that player.pauseVideo is undefined.
奇怪的是,这似乎只是UIWebView中的一个问题(或者可能只是iOS)如果我加载相同的桌面浏览器播放器中的页面.pauseVideo工作正常。此外,当我在浏览器中检查播放器对象时,它表示它是一个Y对象,但在UIWebView中它是一个S对象,并且它的大多数方法都有模糊的名称。
Strangely this only appears to be a problem in the UIWebView (or possibly just iOS) If I load the same page in a desktop browser player.pauseVideo works fine. Furthermore when I inspect the player object in a browser, it says it is a "Y" object, but in the UIWebView it is an "S" object, and most of its methods have obfuscated names.
任何帮助都将不胜感激。
Any help would be appreciated.
更新:我找到了一个使用 player.getIframe()。contentDocument.querySelector('video')。pause()
但这并不理想。
UPDATE: I found a workaround where I use player.getIframe().contentDocument.querySelector('video').pause()
but this is not ideal.
推荐答案
我也遇到了一些麻烦。在搜索SO并重新阅读YouTube iFrame API几次之后,我就会想到在UIWebView中暂停加载到iFrame中的YouTube视频。
I had some trouble with this also. After hunting around SO and re-reading the YouTube iFrame API a few times, here's what I came up with to pause a YouTube video loaded in an iFrame within a UIWebView.
我在一个分页的UIScrollView中有很多UIWebView,所以我有一个方法,我在UIWebView中加载一个HTML字符串:
I have a number of UIWebViews in a a paginated UIScrollView, so I have a method where I form an HTML string to be loaded in a UIWebView:
NSString *urlString = [NSString stringWithFormat:@"https://www.youtube.com/embed/%@",videoID];
preparedHTML = [NSString stringWithFormat:@"<html><body style='background:none; text-align:center;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>var player; function onYouTubeIframeAPIReady(){player=new YT.Player('player')}</script><iframe id='player' class='youtube-player' type='text/html' width='%f' height='%f' src='%@?rel=0&showinfo=0&enablejsapi=1' style='text-align:center; border: 6px solid; border-radius:5px; background-color:transparent;' rel=nofollow allowfullscreen></iframe></body></html>", 628.0f, 352.0f, urlString];
您可以忽略preparedHTML字符串中的样式内容。重要的方面是:
You can ignore the styling stuff in the preparedHTML string. The important aspects are:
- 使用API创建YT.player对象。有一次,我只有iFrame标签中的视频,这阻止了我以后用JS引用播放器对象。
- 我在网上看到了一些例子省略了第一个脚本标记(带有iframe_api src标记的标记),但我确实需要这样才能使其正常工作。
- 在API脚本的开头创建player变量。我也看到一些省略该行的例子。
- 将id标记添加到要在API脚本中引用的iFrame中。我差点忘了这一部分。
- 将enablejsapi = 1添加到iFrame src标记的末尾。这让我挂了一段时间,因为我最初把它作为iFrame标签的一个属性,它不起作用/不起作用。
- Using the API to create the "YT.player" object. At one point, I only had the video in the iFrame tag and that prevented me from referencing the "player" object later with JS.
- I've seen a few examples on the web where the first script tag (the one with the iframe_api src tag) is omitted, but I definitely needed that to get this working.
- Creating the "player" variable at the beginning of the API script. I have also seen some examples that have omitted that line.
- Adding an id tag to the iFrame to be referenced in the API script. I almost forgot that part.
- Adding "enablejsapi=1" to the end of the iFrame src tag. That hung me up for a while, as I initially had it as an attribute of the iFrame tag, which does not work/did not work for me.
当我需要暂停视频时,我只是运行:
When I need to pause the video, I just run this:
[webView stringByEvaluatingJavaScriptFromString:@"player.pauseVideo();"];
我可能正在那里做一些不必要的事情,但这是最终开始工作的我。希望它有所帮助!
I'm probably doing some things in there that are unneccessary, but this is what finally started working for me. Hope it helps!
这篇关于在UIWebView中未定义Youtube iFrame API pauseVideo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!