Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play youtube videos in fullscreen auto

I am trying to play a youtube video using youtube embedded player in my ipad app. I want that as soon as the user clicks on the video thumbnail the video automatically should be loaded fullscreen on switching to landscape mode in ipad. Currently the user has to click the fullscreen button to play the video in fullscreen mode. I want the same effect that is seen when we play the video using the default youtube app that comes with ipad.

This is the code that I am using 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 anybody know if there is a reason or documentation supporting this decision.

like image 595
Aisha Avatar asked Dec 03 '10 11:12

Aisha


2 Answers

You can't, it's not allowed :)

like image 128
Yehonatan Avatar answered Oct 08 '22 18:10

Yehonatan


I was able to access this functionality from a subview titled FigPluginView within the youtube player's webview, using the iOS youtube helper iOS-youtube-player-helper. FigPluginView class dump reference

Create a custom UIView class header that exposes the following method:

#import <Foundation/Foundation.h> 
@interface WMFigPluginView : NSObject

-(void)scriptEnterFullScreen;

@end

Import the custom FigPluginView and YouTube player to the ViewController interface()

    #import <youtube-ios-player-helper/YTPlayerView.h>
    #import "WMFigPluginView.h"

    @property(nonatomic, strong) YTPlayerView *playerView;

Use the following methods to init the youtube player, monitor state change, find and call the fullscreen script within the 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!

like image 44
Casey Avatar answered Oct 08 '22 17:10

Casey