-1

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.

JAL
  • 41,701
  • 23
  • 172
  • 300
Surbhi Garg
  • 414
  • 5
  • 19

2 Answers2

1

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

Community
  • 1
  • 1
HardikDG
  • 5,892
  • 2
  • 26
  • 55
1

@Pyro

setAllowsInlineMediaPlayback works only for ipad, I have already enabled it. and it does now worked, It is not allowing to play the video.

Image after implementation

Surbhi Garg
  • 414
  • 5
  • 19
  • 1
    I don't think it's issue from the implementation side, does this video works with YTPlayerView and not working with iframe implementation?, please check on youtube if this has some permission/region specific issues – HardikDG Mar 27 '16 at 19:01
  • Yes all videos worked on YTPlayerView. These videos work on normal full screen mode but not with embedded player – Surbhi Garg Mar 27 '16 at 19:17
  • ok, can you please provide me the video id so i can check from my side – HardikDG Mar 27 '16 at 19:22
  • _8aKKEjIO2g is one of them – Surbhi Garg Mar 27 '16 at 19:25
  • 1
    ok , i will check if i found something but this video is even not playing in browser(https://www.youtube.com/embed/_8aKKEjIO2g) or YTPlayerView for me – HardikDG Mar 27 '16 at 19:30
  • 3bMYgo_S0Kc this is also one which is not working https://m.youtube.com/watch?v=3bMYgo_S0Kc converted url is http://www.youtube.com/watch?v=3bMYgo_S0Kc&feature=player_embedded – Surbhi Garg Mar 27 '16 at 19:38