How to embed YouTube video on iOS and play it directly on UIWebView without full screen?

前端 未结 2 1493
攒了一身酷
攒了一身酷 2020-11-29 00:55

I am using this code to play YouTube videos on iOS

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame
{
    NSString *htmlString = [NSString stri         


        
相关标签:
2条回答
  • 2020-11-29 01:27

    In Swift:

    let webView = UIWebView(frame:CGRectMake(10, 10,300, 200))
        webView.allowsInlineMediaPlayback = true
        webView.mediaPlaybackRequiresUserAction = false
        self.view.addSubview(self.webView)
    
        let embedHTML = "<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',{events:{onReady:onPlayerReady}})"
        "}"
        "function onPlayerReady(a)"
        "{ "
        "   a.target.playVideo(); "
        "}"
        "</script>"
        "   <iframe id='playerId' type='text/html' width='300' height='200' src='http://www.youtube.com/embed/JW5meKfy323?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'>"
        "        </body>"
        "</html>"
        webView.loadHTMLString(embedHTML, baseURL:NSBundle.mainBundle().resourceURL!)
    
    0 讨论(0)
  • 2020-11-29 01:42

    If anyone is still facing this problem, below is by far the best solution I have seen. Works like a charm.

    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10,300, 200)];
            [self.webView setAllowsInlineMediaPlayback:YES];
            [self.webView setMediaPlaybackRequiresUserAction:NO];
    
            [self.view addSubview:self.webView];
    
            NSString* 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',{events:{onReady:onPlayerReady}})\
                                                }\
                                                function onPlayerReady(a)\
                                                { \
                                                    a.target.playVideo(); \
                                                }\
                                            </script>\
                                            <iframe id='playerId' 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>", 300, 200, @"JW5meKfy3fY"];
            [self.webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];
    

    Source: https://code.google.com/p/gdata-issues/issues/detail?id=5204

    0 讨论(0)
提交回复
热议问题