我需要使用 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 版本内联或第一个版本自动播放?或者有其他解决方案吗?