Saturday 16 August 2014

Play youtube video in UIWebView in Landscape Left mode while whole app in Portrait mode in iOS 7


// AppDelegate.m
// AppDelegate.m 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 // for youtube video playing ...for youtube video play change orientation
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(forceToLandscape:)
                                                 name:@"youTubeChangeOrientationsNotification"
                                               object:nil];

    
    return YES;
} 

#pragma mark - for youtube video play change orientation
//// -- for youtube video play change orientation <START>
int shouldShowInLandscape = 0;
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        return UIInterfaceOrientationMaskLandscapeLeft;
    } else {
        if(shouldShowInLandscape)
        {
            return UIInterfaceOrientationMaskLandscape;
        }
        return UIInterfaceOrientationMaskPortrait;
    }
    
}

-(void) forceToLandscape:(NSNotification*) theNot {
    shouldShowInLandscape = [[theNot object] intValue];    
}
////-- for youtube video play change orientation <END>

VideoViewController.h
//VideoViewController.h
@interface VideoViewController : UIViewController {
    IBOutlet UIWebView *webViewForVideoPlay;
}

VideoViewController.m
//VideoViewController.m 
- (void)viewDidLoad{
    [super viewDidLoad];

// You tube video play
    NSString *youTubeID = [self extractYoutubeID:@"http://www.youtube.com/watch?v=LWSzi-Eae08&feature=youtu.be"];
    [webViewForVideoPlay setAllowsInlineMediaPlayback:YES];
    [webViewForVideoPlay setMediaPlaybackRequiresUserAction:NO];
    [webViewForVideoPlay.scrollView setScrollEnabled:NO];
    NSString *embedHTML =[NSString stringWithFormat:@"\
                          <html><head>\
                          <style type=\"text/css\">\
                          body {\
                          background-color: #000;\
                          }\
                          </style>\
                          </head><body style=\"margin:0\">\
                          <iframe width=\"%f\" height=\"%f\" title=\"YouTube Video\" frameborder=\"0\" class=\"youtube-player\" src=\"http://www.youtube.com/embed/%@?modestbranding=1&amp;rel=0;autoplay=1;showinfo=0;loop=1;autohide=1\" allowfullscreen></iframe>\
                          </body></html>",webViewForVideoPlay.frame.size.width-4, webViewForVideoPlay.frame.size.height,youTubeID];
    [webViewForVideoPlay loadHTMLString:embedHTML baseURL:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerWillExitFullscreen:)
                                                 name:@"UIMoviePlayerControllerWillExitFullscreenNotification"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerDidEnterFullscreen:)
                                                 name:@"UIMoviePlayerControllerDidEnterFullscreenNotification"
                                               object:nil];
    

}
#pragma mark - youtube video play
////-- for you tube video play <START>
- (NSString *)extractYoutubeID:(NSString *)youtubeURL
{
    //NSLog(@"youtube  %@",youtubeURL);
    NSError *error = NULL;
    NSString *regexString = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:&error];
    NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:youtubeURL options:0 range:NSMakeRange(0, [youtubeURL length])];
    if(!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)))
    {
        NSString *substringForFirstMatch = [youtubeURL substringWithRange:rangeOfFirstMatch];
        return substringForFirstMatch;
    }
    return nil;
}
- (void)playerWillExitFullscreen:(NSNotification *)notification {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"youTubeChangeOrientationsNotification"
                                                        object:[NSString stringWithFormat:@"0"]];
    
}

- (void)playerDidEnterFullscreen:(NSNotification *)notification {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"youTubeChangeOrientationsNotification"
                                                        object:[NSString stringWithFormat:@"1"]];
}
////-- for you tube video play <END>

1 comment:

  1. Very useful. Works fine. I have spent one day for this.But youtube videos are not playing automatically. If you don't mind can you add demo project.

    Thanks

    ReplyDelete