Search code examples
iosobjective-cyoutube-iframe-apiytplayerview

How to play Youtube Video from a browser app on non full screen in Objective C


I am working on Browser app, I want when user searches any video on youtube.com the video is playing on full screen but I want the video to be open on a fixed size small frame, Say Half of phone screen size.

I have Video url and video id. I have integrated YOUTube API but as soon as I hit play button on youtube player it plays on full screen.

Please find codebase of integration

self.playerView = [[YTPlayerView alloc]init];
self.playerView.frame = webView.frame;
[self.playerView loadWithVideoId:str];

[webView.superview addSubview:self.playerView];

Please find the screenshots When youtube player is added and After tap on play button

Help will be much appreciated.


Solution

  • Does you project have Controller named MainViewController , that controller should have issue with YTPlayerView, please check for this view in your view controller.
    in most of the cases it will not defined in the class you have directly use in the code


    Searching for the issue in your code i found a property of UIWebview which will help you in playing video inline setAllowsInlineMediaPlayback

    With help of this you can directly play the youtube video in the webview, Here is code sample which you can use to play youtube video

     NSString *strVideoId = @"M7lc1UVf-VE";
     NSString *videoURL = [NSString stringWithFormat:@"https://www.youtube.com/embed/%@?feature=player_detailpage&playsinline=1",strVideoId];
    
     NSString* embedHTML = [NSString stringWithFormat:@"\
                               <html><head>\
                               <style type=\"text/css\">\
                               iframe {position:absolute; top:50%%; margin-top:-130px;}\
                               body {\
                               background-color: transparent;\
                               color: white;\
                               }\
                               </style>\
                               </head><body style=\"margin:0\">\
                               <iframe width=\"100%%\" height=\"240px\" src=\"%@\" frameborder=\"0\"></iframe>\
                               </body></html>",videoURL];
    
    [self.mainwebview setAllowsInlineMediaPlayback:true];
    [self.view addSubview:self.mainwebview];
    [self.mainwebview loadHTMLString:embedHTML baseURL:nil];
    

    In this code you can pass any video id which you were passing in the loadWithVideoId of YTPlayerView
    To change the width/height you can make change in the width=\"100%%\" height=\"240px\" portion of the iframe

    It is working for me in the iPhone also which is looking like this: iframe youtube play in iPhone Another reference the setAllowsInlineMediaPlayback working in iPhone: https://stackoverflow.com/a/15189889/4557505

    The image which you shown in the screenshot looks like particular video issue Note This code does not require YTPlayerView so if this code works for you, you can remove the YTPlayerView from your project if this is not used anywhere else


    Update Some video not playing: I think this issue has not nothing to do with the specific implementation as for me it's not working in browser(with embed Url which we are setting in iFrame) or YTPlayerview also
    But till than i check and may be find some issue in implementation and as you have mentioned:

    Yes all videos worked on YTPlayerView. These videos work on normal full screen mode but not with embedded player

    If you want to make implementation in the YTPlayerview you can check the following code:

    NSString *strVideoId = @"3bMYgo_S0Kc";
    self.playerView = [[YTPlayerView alloc]init];
    self.playerView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height/2);
    [self.playerView loadWithVideoId:strVideoId playerVars:@{@"playsinline":@1}];
    [_mainwebview addSubview:self.playerView];
    [self.mainwebview setAllowsInlineMediaPlayback:true];
    [self.view addSubview:self.mainwebview];
    

    In the loadWithVideoId we can provide the inline parameter which can solve the problem of full screen and this may work for you on the iPhone also

    Note this video is not working on YTPlayer or in browser(embedded url) for me but based on your comment you can try this