我需要使用IOS 5.0+在uiwebview中从youtube自动播放内联视频。 我设法让它内联:
NSString *html = [NSString stringWithFormat:@"\
<html>\
<head>\
<script type='text/javascript'>\
function onPlayerReady(event) {\
event.target.playVideo();\
}\
</script>\
<style type=\"text/css\">\
iframe {position:absolute; top:0%%; margin-top:-0px;}\
body {background-color:#000; margin:0;}\
</style>\
</head>\
<body>\
<iframe width=\"100%%\" src=\"https://www.youtube.com/embed/%@?feature=player_detailpage& modestbranding=1&rel=0;autoplay=1;showinfo=0;loop=1;autohide=1;playsinline=1;autoplay=1\" frameborder=\"0\" allowfullscreen></iframe>\
</body>\
</html>", ID];
[videoWebView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
但我无法弄清楚如何让它自动播放。 我还找到了自动播放视频的其他解决方案,但它以全屏方式启动,我不知道如何使其内联。
NSString *youTubeVideoHTML = @"<!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:'%d', height: '%d', videoId:'%@', events: { 'onReady': onPlayerReady, } }); }
function onPlayerReady(event) { event.target.playVideo(); }
</script>
</body> </html>";
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, 320, 150, ID];
[videoWebView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
有没有办法让js版内联或第一版自动播放?或者还有其他解决方案吗?
答案 0 :(得分:3)
我终于找到了它。 在JS版本中,您可以指定玩家参数(例如&#34; showinfo&#34;,&#34; rel&#34;当然&#34; playinline&#34;)
您只需添加playerVars
对象并在其中指定参数即可。
所以代码看起来像这样:
NSString *youTubeVideoHTML = @"<!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:'%d', height: '%d', videoId:'%@', playerVars: {'playsinline' : 1, 'rel':0, 'showinfo':0}, events: { 'onReady': onPlayerReady, } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, 320, 180, ID];
[videoWebView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
答案 1 :(得分:0)
为什么你没有使用原生视频播放器?
试试LBYouTubeView包装器,它很容易集成。