Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
857 views
in Technique[技术] by (71.8m points)

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

I am using this code to play YouTube videos on iOS

- (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.

Any clue of how making that? Thansk in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...