【问题标题】:UIWebView plays inline without autoplay not work by using iframeUIWebView 在没有自动播放的情况下内联播放,使用 iframe 不起作用
【发布时间】:2014-04-26 23:13:10
【问题描述】:

这些天我搜索这个问题,但我找不到解决方案。我在 UIWebview 中使用 youtube 和 iframe。我想通过以下代码使其在不自动播放的情况下直接播放

embedHTML = [NSString stringWithFormat:@"\
                     <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')\
                     }\
                     </script>\
                     <iframe id='playerId' type='text/html' width='%d' height='%d' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1&autoplay=0' frameborder='0'>\
                     </body>\
                     </html>", (int)myWidth, (int)myHeight, _youtubeCode];

但它不起作用。如果使用此代码,则单击 webview 播放视频,它允许弹出视频播放器播放视频。

我只能通过以下代码成功播放视频,但它必须自动播放视频

 embedHTML = [NSString stringWithFormat:@"\
                               <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('playerLiveId',{events:{onReady:onPlayerReady}})\
                               }\
                               function onPlayerReady(a)\
                               { \
                               a.target.playVideo(); \
                               }\
                               </script>\
                               <iframe id='playerLiveId' type='text/html' width='%d' height='%d' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'>\
                               </body>\
                               </html>", (int)myWidth, (int)myHeight, _youtubeCode];

我需要你的帮助。顺便说一句,对不起我的英语。

【问题讨论】:

    标签: ios iframe uiwebview youtube-api


    【解决方案1】:

    您是否尝试添加以下内容:

    webView.allowsInlineMediaPlayback = YES;
    

    到你的 UIWebview 代码?

    【讨论】:

      【解决方案2】:

      我也有同样的问题。如果使用 Youtube API 播放视频,则可以正常播放。但如果用户按下大红色图标,播放器将进入全屏。我通过添加覆盖视图来修复它并在播放后将其删除。

      UIView *overlayer = [[UIView alloc] initWithFrame:ytWebView.bounds];
      [overlayer setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
      [overlayer setBackgroundColor:[UIColor clearColor]];
      [ytWebView addSubview:overlayer];
      UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playVideo:)];
      [overlayer addGestureRecognizer:tap];
      self.overlayer = overlayer;
      

      在您的playVideo: 中,您使用 Youtube API 播放视频并记住删除覆盖层。 希望得到帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-01
        • 2018-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-18
        • 2018-12-17
        • 2013-08-19
        相关资源
        最近更新 更多