如何在iOS上嵌入YouTube视频并直接在UIWebView上全屏播放?

时间:2021-04-12 18:58:38

I am using this code to play YouTube videos on iOS

我正在使用此代码在iOS上播放YouTube视频

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame
{
    NSString *htmlString = [NSString stringWithFormat:@"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = yes, width = 320\"/></head><body style=\"background:#00;margin-top:0px;margin-left:0px\"><div><object width=\"320\" height=\"180\"><param name=\"movie\" value=\"http://www.youtube.com/v/%@&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/%@&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"180\"></embed></object></div></body></html>", urlString, urlString];

    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
    [videoView loadHTMLString:htmlString baseURL:nil];
    [self.view addSubview:videoView];
    [videoView release];
}

It works like charm but I want a different behaviour. Now a thumbnail of the video appears on the webview (nice!) but when I tap the play icon it opens in full screen. I need that playback is done in the same window since I need to show more stuff.

它像魅力一样,但我想要一个不同的行为。现在,视频的缩略图显示在webview上(很好!)但是当我点击播放图标时,它会全屏打开。我需要在同一窗口中完成播放,因为我需要显示更多内容。

Any clue of how making that? Thansk in advance

有关如何制作的任何线索? Thansk提前

2 个解决方案

#1


27  

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

#2


0  

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!)

#1


27  

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

#2


0  

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!)