0

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

4

2 回答 2

3

我终于找到了。在 JS 版本中,您可以指定播放器参数(如“showinfo”、“rel”,当然还有“playsinline”)

您只需要添加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]];
于 2013-04-04T12:08:07.783 回答
0

为什么您不使用本机视频播放器?
试试LBYouTubeView包装器,它很容易集成。

于 2013-04-04T12:11:57.253 回答