gpt4 book ai didi

javascript - 让 HTML5 YouTube 播放器适应 UIWebView

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:52 25 4
gpt4 key购买 nike

我曾经(在 iOS 6 中)能够“告诉”YouTube 播放器它应该拉伸(stretch)到什么宽度和高度,这没问题。

此代码之前在 iOS 6 中可以正常工作,但现在在 iOS 7 中它根本不起作用

但现在我必须为高度和宽度输入比屏幕本高度得多(3 倍)的值,但这当然不是解决方案,因为设备的尺寸各不相同。

这是按下按钮时运行的代码

NSString *player = @"<!DOCTYPE html>
<html>
<head><style>body{margin:0px 0px 0px 0px;}</style></head>
<body>

<div id=\"player\"></div>

<script>
var tag = document.createElement('script');
tag.src = \"http://www.youtube.com/player_api\";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;

function onYouTubePlayerAPIReady()
{
player = new YT.Player('player', { width:'%0.0fpx', height:'%0.0fpx', videoId:'%@',
playerVars: {playsinline : 1, rel:0, showinfo:0}, events: { 'onReady': onPlayerReady, } });
}
function onPlayerReady(event) { event.target.playVideo();
}
</script>
</body>
</html>"; //End of player creation string

//Then I create the html with the size of the screen
NSString *html = [NSString stringWithFormat:player,[[UIScreen mainScreen] bounds].size.width , [[UIScreen mainScreen] bounds].size.height , @"5bCRDI1BESc"];

//And then I load the html into the webview
[self.webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];

由此生成的 HTML 如下

<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> 
<body>
<iframe id="player" frameborder="0" allowfullscreen="1" title="YouTube video player" width="320px" height="480px" src="http://www.youtube.com/embed/5bCRDI1BESc?playsinline=1&amp;rel=0&amp;showinfo=0&amp;enablejsapi=1"></iframe> <script src="http://s.ytimg.com/yts/jsbin/www-widgetapi-vflvlw_TO.js" async=""></script><script src="http://www.youtube.com/player_api"></script><script> var tag = document.createElement('script'); tag.src = "http://www.youtube.com/player_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player; function onYouTubePlayerAPIReady() {
player = new YT.Player('player', { width:'320px', height:'480px',
videoId:'5bCRDI1BESc',
playerVars: {playsinline : 1, rel:0, showinfo:0},
events: { 'onReady': onPlayerReady, } });
}
function onPlayerReady(event) { event.target.playVideo(); } </script> </body></html>

但结果与 iOS 6 中的结果不同,iOS 6 中播放器填写到 whitespce(webview)中。这是一张图片

Player not filling out into space

如果我然后加倍(屏幕尺寸!) 到 640px 和 960px 进入 HTML 的宽度和高度!?

double the screen size not either filling out into space

有人知道 iOS 7 中的 YouTube API 或 Safari 浏览器发生了什么吗?

最佳答案

这是我当前的实现:

let webView = UIWebView(...)

// get the ID of the video you want to play
let videoID = "zN-GGeNPQEg" // https://www.youtube.com/watch?v=zN-GGeNPQEg

// Replace the height and width of the player here to match your UIWebView's frame rect
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 your webView with the HTML we just set up
webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL)

这是 UIViewController 的内部,我将 View Controller 的 view 的框架作为播放器的宽度和高度传递。您应该相应地调整播放器的大小。

关于javascript - 让 HTML5 YouTube 播放器适应 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22283514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com