Play YouTube videos in full screen

I am trying to play youtube video using the youtube built-in player in my ipad application. I want that as soon as the user clicks on the thumbnail of the video, the video should automatically be loaded in full screen mode when switching to landscape mode in ipad. Currently, the user needs to click the full screen button to play the video in full screen. I need the same effect that is observed when playing video using the default youtube app that comes with the ipad.

This is the code that I use to play you tube video on ipad.

embedHTML = @"<object width=\"640\" height=\"390\"> <param name=\"movie\"value=\"http://www.youtube.com/v/IYX_Ql-3U10&fs=1\"> <param name=\"allowFullScreen\" value=\"true\"></param> <param name=\"allowScriptAccess\" value=\"always\"></param> <embed id=\"yt\" src=\"http://www.youtube.com/v/youtube_video_id&fs=1\" type=\"application/x-shockwave-flash\" position=\"fixed\" allowfullscreen=\"true\" allowScriptAccess=\"always\" width=\"640\" height=\"390\"></embed></object>"; 

And if this is not possible, does anyone know if there is a reason or documentation supporting this solution.

+6
objective-c youtube ipad
source share
4 answers

You cannot, this is not allowed :)

+5
source share

I managed to access this function from a subheading called "Figure PuluginView" in the youtube player webview using the youtube iOS assistant iOS-youtube-player-helper . Link to the FigPluginView class dump

Create your own UIView class header, which provides the following method:

 #import <Foundation/Foundation.h> @interface WMFigPluginView : NSObject -(void)scriptEnterFullScreen; @end 

Import the FigPluginView and YouTube custom element into the ViewController () interface

  #import <youtube-ios-player-helper/YTPlayerView.h> #import "WMFigPluginView.h" @property(nonatomic, strong) YTPlayerView *playerView; 

Use the following methods to start the YouTube player, change the status of the monitor, search for and call the full-screen script in FigPluginView.

 - (void)presentYoutubeVideo { if ([self.view.subviews containsObject:_playerView]){ [_playerView removeFromSuperview]; } CGRect playerFrame = CGRectMake(0, 0, 0, 0); _playerView = [[YTPlayerView alloc]initWithFrame:playerFrame]; _playerView.delegate = self; [_playerView.webView setAllowsInlineMediaPlayback: NO]; [self.view addSubview:_playerView]; [self.playerView loadWithVideoId:@"YouTubeVideoID" playerVars:@{@"showinfo":@0,@"modestbranding":@1,@"autoplay":@1,@"playsinline":@0}]; } -(void)playerViewDidBecomeReady:(YTPlayerView *)playerView { [_playerView playVideo]; } -(void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state { //stop activity indicator if (state == kYTPlayerStateBuffering) { //start activity indicator } if (state == kYTPlayerStatePlaying) { WMFigPluginView *figPluginView = (WMFigPluginView*)[self findFigPluginView:_playerView.webView]; [figPluginView scriptEnterFullScreen]; } } - (UIView *)findFigPluginView:(UIView *)view { for (__unsafe_unretained UIView *subview in view.subviews) { if ([NSStringFromClass(subview.class) hasSuffix:@"FigPluginView"]) { return subview; } else if (subview.subviews.count > 0) { return [self findFigPluginView:subview]; } } return nil; } 

Hope this helps!

+4
source share

There seems to be no easy solution. If my client wants this (I will find out in a few days :-)), I will probably try to open a new controller with a web view that will cover the entire screen, and then auto -play it, as in this post: How to make a YouTube video automatically playable inside UIWebView

0
source share

As for the authors of other answers, it is quite possible to do this using UIWebViews in iOS and without using private APIs. Here is a pretty simple way to do this.

 - (void)playYoutubeVideo:(NSString *)videoId { UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero]; webView.center = self.view.center; //Technically not needed, I've found it looks better with this added (when the video is done playing it looks better while being minimized) webView.mediaPlaybackRequiresUserAction = NO; [self.view addSubview:webView]; NSString *youTubeVideoHTML = [NSString stringWithFormat:@"<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'768', height:'1024', videoId:'%@', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>", videoId]; [webView loadHTMLString:youTubeVideoHTML baseURL: [NSBundle mainBundle].bundleURL]; } 

The essence of this implementation is to use a UIWebView with a CGRectZero frame along with custom HTML / Javascript that handles auto-play of video. With a UIWebView with a CGRectZero frame, the user does not see any annoying intermediate page before the video is automatically played, and HTML / Javascript makes a heavy lift related to getting the page to automatically play the video.

There is a slight delay before the video is presented. I did not find a way to eliminate this pair of second delay, unfortunately.

0
source share

All Articles