UIWebView在不使用自动播放的情况下播放内联使用iframe无法正常工作

时间:2014-03-20 08:32:21

标签: ios iframe uiwebview youtube-api

我现在搜索这个问题,但我无法找到解决方案。我在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];

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

2 个答案:

答案 0 :(得分:0)

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

webView.allowsInlineMediaPlayback = YES;

到您的UIWebview代码?

答案 1 :(得分:0)

我有同样的问题。如果使用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播放视频并记住删除叠加层。 希望肝脏!