New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make YTPlayerView fullscreen programatically #35

Closed
yadavharsh opened this Issue Sep 30, 2014 · 29 comments

Comments

Projects
None yet
@yadavharsh

yadavharsh commented Sep 30, 2014

Hi,

I am trying to make the player view full screen programmatically, but I am unable to do so.

Is there an exposed method in the api or a callback that can be used to accomplish this?

Thanks

@matheusmaaz

This comment has been minimized.

Show comment
Hide comment
@matheusmaaz

matheusmaaz Oct 3, 2014

I'm not sure I understood your question. However, you could try to change the html file. Try to add: body{ margin: 0; width:100%%; height:100%%; } html { width:100%%; height:100%%; }

matheusmaaz commented Oct 3, 2014

I'm not sure I understood your question. However, you could try to change the html file. Try to add: body{ margin: 0; width:100%%; height:100%%; } html { width:100%%; height:100%%; }

@yadavharsh

This comment has been minimized.

Show comment
Hide comment
@yadavharsh

yadavharsh Oct 3, 2014

Hello Matheus,

Thanks for the reply.

What I mean is that, I want to be able to make the player fullscreen, on
click of a button, different than the player-fullscreen control.

In other scenario, I want the video play inline in portrait mode, but when
I switch to landscape mode, I want the player to go fullscreen.

Thanks,
Harsh

On Fri, Oct 3, 2014 at 6:46 AM, Matheus Maaz notifications@github.com
wrote:

I'm not sure I understood your question. However, you could try to change
the html file. Try to add:

body { margin: 0; width:100%%; height:100%%; }
html {
width:100%%; height:100%%; }


Reply to this email directly or view it on GitHub
#35 (comment)
.

yadavharsh commented Oct 3, 2014

Hello Matheus,

Thanks for the reply.

What I mean is that, I want to be able to make the player fullscreen, on
click of a button, different than the player-fullscreen control.

In other scenario, I want the video play inline in portrait mode, but when
I switch to landscape mode, I want the player to go fullscreen.

Thanks,
Harsh

On Fri, Oct 3, 2014 at 6:46 AM, Matheus Maaz notifications@github.com
wrote:

I'm not sure I understood your question. However, you could try to change
the html file. Try to add:

body { margin: 0; width:100%%; height:100%%; }
html {
width:100%%; height:100%%; }


Reply to this email directly or view it on GitHub
#35 (comment)
.

@Eppes

This comment has been minimized.

Show comment
Hide comment
@Eppes

Eppes Oct 7, 2014

thanks for your sharing~

i've some question with yadavharsh, how can i set play video landscape mode when user playing video but my app device orientation is setting only support portrait mode.( i try it as my mention, but only portrait, not suppot landscape mode when playing), thank you very much.

Eppes commented Oct 7, 2014

thanks for your sharing~

i've some question with yadavharsh, how can i set play video landscape mode when user playing video but my app device orientation is setting only support portrait mode.( i try it as my mention, but only portrait, not suppot landscape mode when playing), thank you very much.

@yadavharsh

This comment has been minimized.

Show comment
Hide comment
@yadavharsh

yadavharsh Oct 7, 2014

Eppes,

You need to support both portrait and landscape orientation for the view-controller displaying the video for it to work.

If you want the other view controllers to be portrait only, implement the following method:

-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

Saying that, how can you make the youtube video full-screen when you go landscape?

yadavharsh commented Oct 7, 2014

Eppes,

You need to support both portrait and landscape orientation for the view-controller displaying the video for it to work.

If you want the other view controllers to be portrait only, implement the following method:

-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

Saying that, how can you make the youtube video full-screen when you go landscape?

@Eppes

This comment has been minimized.

Show comment
Hide comment
@Eppes

Eppes Oct 8, 2014

hi, yadavharsh
In my project, i'm drag a 320x320 UIView(as playerView) to my storyboard, follow youtube-ios-player-helper's installation step and run my project. When i tap playerView layout will pop full-screen play mode whether phone orientation is portrait or landscape mode. In playing video, you can double tap play screen will zoom video view.

Add matheusmaaz mention code in YTPlayerView-iframe-player.html

<style> body{ margin: 0; width:100%%; height:100%%; } html { width:100%%; height:100%%; } </style>

will adjust video display screenshot to fit my UIView(320x320) in viewController, default display screenshot size less than 320x320. I'm no sure i can slove your problem, Big thanks your reply.

let me ask a question ,how to force playing video always landscape mode whether i setting device orientation is portrait, landscape Left or landscape Right?

Eppes commented Oct 8, 2014

hi, yadavharsh
In my project, i'm drag a 320x320 UIView(as playerView) to my storyboard, follow youtube-ios-player-helper's installation step and run my project. When i tap playerView layout will pop full-screen play mode whether phone orientation is portrait or landscape mode. In playing video, you can double tap play screen will zoom video view.

Add matheusmaaz mention code in YTPlayerView-iframe-player.html

<style> body{ margin: 0; width:100%%; height:100%%; } html { width:100%%; height:100%%; } </style>

will adjust video display screenshot to fit my UIView(320x320) in viewController, default display screenshot size less than 320x320. I'm no sure i can slove your problem, Big thanks your reply.

let me ask a question ,how to force playing video always landscape mode whether i setting device orientation is portrait, landscape Left or landscape Right?

@JesusSpiegel

This comment has been minimized.

Show comment
Hide comment
@JesusSpiegel

JesusSpiegel Oct 10, 2014

My view controller is playing the video inside an small frame and full screen button works fine. However, I would like to launch full screen mode when directly moving to landscape, without the need of interacting with the full screen button.

img_0301

Is that possible?

JesusSpiegel commented Oct 10, 2014

My view controller is playing the video inside an small frame and full screen button works fine. However, I would like to launch full screen mode when directly moving to landscape, without the need of interacting with the full screen button.

img_0301

Is that possible?

@yadavharsh

This comment has been minimized.

Show comment
Hide comment
@yadavharsh

yadavharsh Oct 13, 2014

@Eppes ,

To always play fullscreen, you need to set the playsinline property to 0. Something like:

    NSDictionary *playerVars = @{
                                 @"playsinline" : @0,
                                 };

    [player loadWithVideoId:movieURL playerVars:playerVars];

@JesusEspejo ,

Thats exactly what my question is. If you know a way, would love to know that.

yadavharsh commented Oct 13, 2014

@Eppes ,

To always play fullscreen, you need to set the playsinline property to 0. Something like:

    NSDictionary *playerVars = @{
                                 @"playsinline" : @0,
                                 };

    [player loadWithVideoId:movieURL playerVars:playerVars];

@JesusEspejo ,

Thats exactly what my question is. If you know a way, would love to know that.

@khaledalissa

This comment has been minimized.

Show comment
Hide comment
@khaledalissa

khaledalissa Oct 20, 2014

Hello yadavharsh,
I guess we can simulate a go-to fullscreen action by doing the following:
When the user clicks the fullscreen button, pause the video, save current time, reload the video with playsinline:0 and set time to current time.

Please note that I didn't try this.

khaledalissa commented Oct 20, 2014

Hello yadavharsh,
I guess we can simulate a go-to fullscreen action by doing the following:
When the user clicks the fullscreen button, pause the video, save current time, reload the video with playsinline:0 and set time to current time.

Please note that I didn't try this.

@JesusSpiegel

This comment has been minimized.

Show comment
Hide comment
@JesusSpiegel

JesusSpiegel Oct 20, 2014

Thanks @yadavharsh and @khaledalissa for your reactions.

Indeed @khaledalissa 's solution is valid. However I have tried it and it takes a few seconds to do all the process. I expected a fast transition, just like MPMoviePlayerController.

Finally, my solution was the following. After the orientation change even, I do the following:
1- Hide StatusBar
2- Hide Navigation Bar
3- Turn video player and change the frame animately to fulfil the whole screen

This way, after getting the new frame the video player will re-load, it will re-adjust the video resolution and finally it will resume the playback.

I hope it helps.

JesusSpiegel commented Oct 20, 2014

Thanks @yadavharsh and @khaledalissa for your reactions.

Indeed @khaledalissa 's solution is valid. However I have tried it and it takes a few seconds to do all the process. I expected a fast transition, just like MPMoviePlayerController.

Finally, my solution was the following. After the orientation change even, I do the following:
1- Hide StatusBar
2- Hide Navigation Bar
3- Turn video player and change the frame animately to fulfil the whole screen

This way, after getting the new frame the video player will re-load, it will re-adjust the video resolution and finally it will resume the playback.

I hope it helps.

@JV17

This comment has been minimized.

Show comment
Hide comment
@JV17

JV17 Oct 27, 2014

@yadavharsh I think you have to set for autoplay and not allow play inline to jump directly to fullscreen..

JV17 commented Oct 27, 2014

@yadavharsh I think you have to set for autoplay and not allow play inline to jump directly to fullscreen..

@ulukaya ulukaya closed this Jan 7, 2015

@thidayatullah

This comment has been minimized.

Show comment
Hide comment
@thidayatullah

thidayatullah Jan 15, 2015

@JesusEspejo : Your solution's work fine. I use this same method in my app using AutoLayout, the only problem is every time I change the orientation the video is re-loading.
I think the video will always reload everytime we change the youtube-player view frame / position either with code or auto layout.

Is there a way to make it continue the video smoothly without reloading? I tried to pause it before the orientation change and resume it after the orientation is changed (and add delay) but it still doesn't work.

And please check the Youtube App on iOS, it have strange behaviour (delayed orientation change) but the video doesn't reload.

Here is my code :

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [self.myVideoPlayer pauseVideo];
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {

     } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {

         UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
         // do whatever
         if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){
             [self.navigationController setNavigationBarHidden:YES animated:YES];
         }else{
             [self.navigationController setNavigationBarHidden:NO animated:YES];
         }
         [self performSelector:@selector(playVideo) withObject:nil afterDelay:1];

     }];

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

thidayatullah commented Jan 15, 2015

@JesusEspejo : Your solution's work fine. I use this same method in my app using AutoLayout, the only problem is every time I change the orientation the video is re-loading.
I think the video will always reload everytime we change the youtube-player view frame / position either with code or auto layout.

Is there a way to make it continue the video smoothly without reloading? I tried to pause it before the orientation change and resume it after the orientation is changed (and add delay) but it still doesn't work.

And please check the Youtube App on iOS, it have strange behaviour (delayed orientation change) but the video doesn't reload.

Here is my code :

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [self.myVideoPlayer pauseVideo];
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {

     } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {

         UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
         // do whatever
         if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){
             [self.navigationController setNavigationBarHidden:YES animated:YES];
         }else{
             [self.navigationController setNavigationBarHidden:NO animated:YES];
         }
         [self performSelector:@selector(playVideo) withObject:nil afterDelay:1];

     }];

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
@JV17

This comment has been minimized.

Show comment
Hide comment
@JV17

JV17 Jan 15, 2015

@thidayatullah it shouldn't reload when you change the frame... If you set the player parameters to play inline properly, it should work fine. I have it in example app playing inline and when I change to landscape, I resized the frame of the player to the entire screen to simulate a fullscreen mode and it doesn't reload or anything like that.. It keeps playing smoothly..

JV17 commented Jan 15, 2015

@thidayatullah it shouldn't reload when you change the frame... If you set the player parameters to play inline properly, it should work fine. I have it in example app playing inline and when I change to landscape, I resized the frame of the player to the entire screen to simulate a fullscreen mode and it doesn't reload or anything like that.. It keeps playing smoothly..

@thidayatullah

This comment has been minimized.

Show comment
Hide comment
@thidayatullah

thidayatullah Jan 15, 2015

@JV17 here is my code to load the video (I put it in viewDidLoad)

    NSDictionary *playerVars = @{
      @"controls" : @1,
      @"playsinline" : @1,
      @"autohide" : @1,
      @"showinfo" : @1,
      @"autoplay" : @0,
      @"fs" : @1,
      @"rel" : @0,
      @"loop" : @0,
      @"enablejsapi" : @1,
      @"modestbranding" : @1
      };
    [self.myVideoPlayer loadWithVideoId:currentClip.clip_video.video_media_id playerVars:playerVars];

Is there something I did wrong?
And thank you for your example app, definitely gonna check it !

thidayatullah commented Jan 15, 2015

@JV17 here is my code to load the video (I put it in viewDidLoad)

    NSDictionary *playerVars = @{
      @"controls" : @1,
      @"playsinline" : @1,
      @"autohide" : @1,
      @"showinfo" : @1,
      @"autoplay" : @0,
      @"fs" : @1,
      @"rel" : @0,
      @"loop" : @0,
      @"enablejsapi" : @1,
      @"modestbranding" : @1
      };
    [self.myVideoPlayer loadWithVideoId:currentClip.clip_video.video_media_id playerVars:playerVars];

Is there something I did wrong?
And thank you for your example app, definitely gonna check it !

@JV17

This comment has been minimized.

Show comment
Hide comment
@JV17

JV17 Jan 15, 2015

Try this..

NSDictionary *playerVars = @{
      @"controls" : @"1",
      @"playsinline" : @"1",
      @"autohide" : @"1",
      @"showinfo" : @"1",
      @"autoplay" : @"0",
      @"fs" : @"1",
      @"rel" : @"0",
      @"loop" : @"0",
      @"enablejsapi" : @"1",
      @"modestbranding" : @"1",
      };
    [self.myVideoPlayer loadWithVideoId:currentClip.clip_video.video_media_id playerVars:playerVars];

JV17 commented Jan 15, 2015

Try this..

NSDictionary *playerVars = @{
      @"controls" : @"1",
      @"playsinline" : @"1",
      @"autohide" : @"1",
      @"showinfo" : @"1",
      @"autoplay" : @"0",
      @"fs" : @"1",
      @"rel" : @"0",
      @"loop" : @"0",
      @"enablejsapi" : @"1",
      @"modestbranding" : @"1",
      };
    [self.myVideoPlayer loadWithVideoId:currentClip.clip_video.video_media_id playerVars:playerVars];
@thidayatullah

This comment has been minimized.

Show comment
Hide comment
@thidayatullah

thidayatullah Jan 15, 2015

@JV17 : Tried it, still reloading.
And also I run your example app, and it is still reloading when I change the orientation.

Maybe I'm not stated it clearly in my first question -my bad-, I try to change the orientation without going to full screen mode.

So it goes like this:
I played my video inline in portrait, and when I change it to landscape I RESIZE the player to fill the screen WITHOUT going to full screen mode that's when reloading happened.

thidayatullah commented Jan 15, 2015

@JV17 : Tried it, still reloading.
And also I run your example app, and it is still reloading when I change the orientation.

Maybe I'm not stated it clearly in my first question -my bad-, I try to change the orientation without going to full screen mode.

So it goes like this:
I played my video inline in portrait, and when I change it to landscape I RESIZE the player to fill the screen WITHOUT going to full screen mode that's when reloading happened.

@JV17

This comment has been minimized.

Show comment
Hide comment
@JV17

JV17 Jan 15, 2015

@thidayatullah I just tested my example app and works fine for me.. it launches the player in inline and it keeps it like that unless you pressed the fullscreen button. When rotation happens keeps playing and resizes the frame.. This is in iOS 8, I haven't test it in iOS 7 or lower.. I will check those versions later on today.

Btw, you shouldn't tell the player to pause or play if is already playing when doing the rotation and resizing it. I just saw that in the code viewWillTransitionToSize: of your previous comment.

JV17 commented Jan 15, 2015

@thidayatullah I just tested my example app and works fine for me.. it launches the player in inline and it keeps it like that unless you pressed the fullscreen button. When rotation happens keeps playing and resizes the frame.. This is in iOS 8, I haven't test it in iOS 7 or lower.. I will check those versions later on today.

Btw, you shouldn't tell the player to pause or play if is already playing when doing the rotation and resizing it. I just saw that in the code viewWillTransitionToSize: of your previous comment.

@JV17

This comment has been minimized.

Show comment
Hide comment
@JV17

JV17 Jan 16, 2015

@thidayatullah I just checked and looks like it happens in iPhone 6 devices and lower.. I will check it out, but I was testing it on an iPhone 6 Plus.. I think is because it changes the quality of the video is why it reloads it..

So, I will try find out how to fix it over the weekend but it seams to be dead end (I don't have a good relationship with Javascript lol).. The only way is loading it with this dimensions height:360px, width:640px, so it loads in HD or at least enough resolution so it won't do the reloading when the frame gets updated every-time it rotates.

JV17 commented Jan 16, 2015

@thidayatullah I just checked and looks like it happens in iPhone 6 devices and lower.. I will check it out, but I was testing it on an iPhone 6 Plus.. I think is because it changes the quality of the video is why it reloads it..

So, I will try find out how to fix it over the weekend but it seams to be dead end (I don't have a good relationship with Javascript lol).. The only way is loading it with this dimensions height:360px, width:640px, so it loads in HD or at least enough resolution so it won't do the reloading when the frame gets updated every-time it rotates.

@Kowaio

This comment has been minimized.

Show comment
Hide comment
@Kowaio

Kowaio Jan 20, 2015

Hi @JV17,

I've got the same issue you discussed with @thidayatullah about the reloading video when changing the player view size (by rotating the device for instance).

Indeed, it seems the YT API forces the reloading when the resolution changes.
In my case, I start to load and play the video with params inline = 1, in portrait mode, on an iPhone 6 (so width=375 and height about 210px at that time with a 16/9 ratio). The YTPlayerView returns a quality of medium at first. I guess it is due to this low resolution.

As soon as I rotate the device and do the trick explains above to rotate the YTPlayerView and resize it to my whole screen (so w=667 h=375 in landscape mode for iPhone6), a BUFFERING state is called by the delegate and the quality is changed to hd720.

I currently can't find a way to force to hd720 even with a low resolution (height = 210 in my case).

Below are my lines in YTPlayerView-iframe-player.html:

<style>
body { margin: 0; width:100%%; height:100%%; }
html { width:100%%; height:100%%; }
</style>

I did that in order for me to have the video size always at the size of my YTPlayerView. It allows me, when rotating my device to landscape to update the video size to the view. I think this is the main cause of my issue.

Indeed, when I changed those lines with 1280px and 720px, for instance, it loads the video with quality = hd720 for both landscape and portrait mode.

But the problem is that, since I forced the resolution to load a better quality, the video inside the YTPlayerView is also in 1280px and 720px and therefore is bigger than the view (which is still 375x210 in portrait, of course). So I kind of need those 100%% there...

I think I'm hitting a wall here.

If you've got (or anyone else) an input on this matter, it would be greatly appreciated :)

Thanks a lot,

My best regards,

Kowaio commented Jan 20, 2015

Hi @JV17,

I've got the same issue you discussed with @thidayatullah about the reloading video when changing the player view size (by rotating the device for instance).

Indeed, it seems the YT API forces the reloading when the resolution changes.
In my case, I start to load and play the video with params inline = 1, in portrait mode, on an iPhone 6 (so width=375 and height about 210px at that time with a 16/9 ratio). The YTPlayerView returns a quality of medium at first. I guess it is due to this low resolution.

As soon as I rotate the device and do the trick explains above to rotate the YTPlayerView and resize it to my whole screen (so w=667 h=375 in landscape mode for iPhone6), a BUFFERING state is called by the delegate and the quality is changed to hd720.

I currently can't find a way to force to hd720 even with a low resolution (height = 210 in my case).

Below are my lines in YTPlayerView-iframe-player.html:

<style>
body { margin: 0; width:100%%; height:100%%; }
html { width:100%%; height:100%%; }
</style>

I did that in order for me to have the video size always at the size of my YTPlayerView. It allows me, when rotating my device to landscape to update the video size to the view. I think this is the main cause of my issue.

Indeed, when I changed those lines with 1280px and 720px, for instance, it loads the video with quality = hd720 for both landscape and portrait mode.

But the problem is that, since I forced the resolution to load a better quality, the video inside the YTPlayerView is also in 1280px and 720px and therefore is bigger than the view (which is still 375x210 in portrait, of course). So I kind of need those 100%% there...

I think I'm hitting a wall here.

If you've got (or anyone else) an input on this matter, it would be greatly appreciated :)

Thanks a lot,

My best regards,

@JV17

This comment has been minimized.

Show comment
Hide comment
@JV17

JV17 Jan 20, 2015

@Kowaio Sorry, but I tried injecting js code and changing different parameters loading them to the player but not luck. Even tho, from my point of view the behaviour of the player is correct, because it avoids extra use of data when you are using the player over 3G/LTE.

JV17 commented Jan 20, 2015

@Kowaio Sorry, but I tried injecting js code and changing different parameters loading them to the player but not luck. Even tho, from my point of view the behaviour of the player is correct, because it avoids extra use of data when you are using the player over 3G/LTE.

@Kowaio

This comment has been minimized.

Show comment
Hide comment
@Kowaio

Kowaio Feb 4, 2015

Fair enough!

However, it seems weird that this works on a browser (google chrome for instance) and not in our case. Even if you're watching a video embed on a website with a low resolution, you still can change the quality to HD and watch it. It seems like the player or the API is blocking it. I understand "the extra use of data" matter, but it should detect if the connection is currently running on Wifi or phone network then. In any case, it could be great to force the quality regardless of the view resolution. Some videos are really quite short (less than 20 seconds) and I don't find it user-friendly to have the player reloading the view each time it goes into landscape or portrait.

Kowaio commented Feb 4, 2015

Fair enough!

However, it seems weird that this works on a browser (google chrome for instance) and not in our case. Even if you're watching a video embed on a website with a low resolution, you still can change the quality to HD and watch it. It seems like the player or the API is blocking it. I understand "the extra use of data" matter, but it should detect if the connection is currently running on Wifi or phone network then. In any case, it could be great to force the quality regardless of the view resolution. Some videos are really quite short (less than 20 seconds) and I don't find it user-friendly to have the player reloading the view each time it goes into landscape or portrait.

@naveen488

This comment has been minimized.

Show comment
Hide comment
@naveen488

naveen488 Feb 9, 2016

sry sir, I am added my question i think you know about how to play youtube videos playing while lock the screen. my problem is how to show progress bar while lock the screen?

pls help me sir..........

naveen488 commented Feb 9, 2016

sry sir, I am added my question i think you know about how to play youtube videos playing while lock the screen. my problem is how to show progress bar while lock the screen?

pls help me sir..........

@glennposadas

This comment has been minimized.

Show comment
Hide comment
@glennposadas

glennposadas Sep 1, 2016

Hi everyone, I've read all of your comments here and apparently I got no absolute solution to the very question of the OP. I got the same question: How to programmatically make the player full screen (e.g. if I tap a UIButton, the screen goes full).???

Glenn

glennposadas commented Sep 1, 2016

Hi everyone, I've read all of your comments here and apparently I got no absolute solution to the very question of the OP. I got the same question: How to programmatically make the player full screen (e.g. if I tap a UIButton, the screen goes full).???

Glenn

@KSelvam

This comment has been minimized.

Show comment
Hide comment
@KSelvam

KSelvam Feb 17, 2017

For auto rotation

Take the height constraint reference of main view where player add as subview and use following code. This worked 100% for me

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

    // Register to receive notification
    NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange(notification:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    
     UIDevice.current.beginGeneratingDeviceOrientationNotifications()
    
}

func deviceOrientationDidChange(notification:Notification)
{
    let orientation : UIInterfaceOrientation = UIApplication.shared.statusBarOrientation
    
    if orientation.isLandscape
    {
        playerHeightConstraint.constant = self.view.frame.size.height
        print("landscape")
    } else
    {
        playerHeightConstraint.constant = 180
        print("portrait")
    }
    
}


override func viewWillDisappear(_ animated: Bool)
{
    super.viewWillDisappear(animated)
    
    UIDevice.current.endGeneratingDeviceOrientationNotifications()
    
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil);

}

KSelvam commented Feb 17, 2017

For auto rotation

Take the height constraint reference of main view where player add as subview and use following code. This worked 100% for me

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

    // Register to receive notification
    NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange(notification:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    
     UIDevice.current.beginGeneratingDeviceOrientationNotifications()
    
}

func deviceOrientationDidChange(notification:Notification)
{
    let orientation : UIInterfaceOrientation = UIApplication.shared.statusBarOrientation
    
    if orientation.isLandscape
    {
        playerHeightConstraint.constant = self.view.frame.size.height
        print("landscape")
    } else
    {
        playerHeightConstraint.constant = 180
        print("portrait")
    }
    
}


override func viewWillDisappear(_ animated: Bool)
{
    super.viewWillDisappear(animated)
    
    UIDevice.current.endGeneratingDeviceOrientationNotifications()
    
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil);

}
@shavik

This comment has been minimized.

Show comment
Hide comment
@shavik

shavik Mar 3, 2017

Hi @JV17 ,
Have you got solution of your problem yet? reloading issue due to high resolution of iPhone 6 plus while changing frame.

Thanks

shavik commented Mar 3, 2017

Hi @JV17 ,
Have you got solution of your problem yet? reloading issue due to high resolution of iPhone 6 plus while changing frame.

Thanks

@supermnemonic

This comment has been minimized.

Show comment
Hide comment
@supermnemonic

supermnemonic Apr 7, 2017

i successfully implemented fullscreen mode by change the height constraint on rotate. but i got reloading problem because the frame is changed and quality also changed. then i came up to this thread #221 and reloading issue is solved.

this is my step:

  1. create and connect (iboutlet) height constraint for video container.
  2. change constraint constant on orientation changed. when it goes landscape height = screen height, when it portrait height back to normal.
  3. to avoid reloading video, use .cueVideo(byId: startSeconds: suggestedQuality: ) before playing video.

(Swift 3)

class VideoDetailController: UIViewController {
    @IBOutlet weak var viewVideoPlayer: YTPlayerView!
    @IBOutlet weak var playerHeightConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        viewVideoPlayer.delegate = self

        let playerVars = [
            "modestbranding" : "1",
            "controls" : "2",
            "playsinline" : "1",
            "autohide" : "1",
            "showinfo" : "0",
            "autoplay" : "1",
            "fs" : "1",
            "rel" : "0",
            "loop" : "0",
            "enablejsapi" : "1",
            "iv_load_policy": "3"
        ]

        viewVideoPlayer.load(withVideoId: "youtubeId", playerVars: playerVars)
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        
        coordinator.animate(alongsideTransition: { _ in
            if UIDevice.current.orientation.isLandscape {
                self.playerHeightConstraint.constant = self.view.frame.size.height
                // you can hide statusbar and navbar here
            } else {
                self.playerHeightConstraint.constant = 230
                // you can show again statusbar and navbar here when back to portrait
            }
        }, completion: nil)
    }
}

extension VideoDetailController: YTPlayerViewDelegate {   
    func playerViewDidBecomeReady(_ playerView: YTPlayerView) {
        playerView.cueVideo(byId: "youtubeId", startSeconds: playerView.currentTime(), suggestedQuality: .medium)
        playerView.playVideo()
    }
}


supermnemonic commented Apr 7, 2017

i successfully implemented fullscreen mode by change the height constraint on rotate. but i got reloading problem because the frame is changed and quality also changed. then i came up to this thread #221 and reloading issue is solved.

this is my step:

  1. create and connect (iboutlet) height constraint for video container.
  2. change constraint constant on orientation changed. when it goes landscape height = screen height, when it portrait height back to normal.
  3. to avoid reloading video, use .cueVideo(byId: startSeconds: suggestedQuality: ) before playing video.

(Swift 3)

class VideoDetailController: UIViewController {
    @IBOutlet weak var viewVideoPlayer: YTPlayerView!
    @IBOutlet weak var playerHeightConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        viewVideoPlayer.delegate = self

        let playerVars = [
            "modestbranding" : "1",
            "controls" : "2",
            "playsinline" : "1",
            "autohide" : "1",
            "showinfo" : "0",
            "autoplay" : "1",
            "fs" : "1",
            "rel" : "0",
            "loop" : "0",
            "enablejsapi" : "1",
            "iv_load_policy": "3"
        ]

        viewVideoPlayer.load(withVideoId: "youtubeId", playerVars: playerVars)
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        
        coordinator.animate(alongsideTransition: { _ in
            if UIDevice.current.orientation.isLandscape {
                self.playerHeightConstraint.constant = self.view.frame.size.height
                // you can hide statusbar and navbar here
            } else {
                self.playerHeightConstraint.constant = 230
                // you can show again statusbar and navbar here when back to portrait
            }
        }, completion: nil)
    }
}

extension VideoDetailController: YTPlayerViewDelegate {   
    func playerViewDidBecomeReady(_ playerView: YTPlayerView) {
        playerView.cueVideo(byId: "youtubeId", startSeconds: playerView.currentTime(), suggestedQuality: .medium)
        playerView.playVideo()
    }
}


@tirrorex

This comment has been minimized.

Show comment
Hide comment
@tirrorex

tirrorex May 16, 2017

Bumping this, is there any way to trigger a notification when the player goes fullscreen using the control?
I checked the code and i still have no idea of what is happening when a user trigger the control.
Also avplayer has a AVFullScreenViewController, is there any equivalent we can use?
That would make things so much easier.

tirrorex commented May 16, 2017

Bumping this, is there any way to trigger a notification when the player goes fullscreen using the control?
I checked the code and i still have no idea of what is happening when a user trigger the control.
Also avplayer has a AVFullScreenViewController, is there any equivalent we can use?
That would make things so much easier.

@shamithmukundan

This comment has been minimized.

Show comment
Hide comment
@shamithmukundan

shamithmukundan Feb 23, 2018

@tirrorex here you can use UIWindowDidBecomeVisible and UIWindowDidBecomeHidden notifications to observe when the player goes full screen

shamithmukundan commented Feb 23, 2018

@tirrorex here you can use UIWindowDidBecomeVisible and UIWindowDidBecomeHidden notifications to observe when the player goes full screen

@tirrorex

This comment has been minimized.

Show comment
Hide comment
@tirrorex

tirrorex Feb 26, 2018

Pretty sure i didn't need it anymore, since ios 11 i found a workaround that suits my usecase better.
Thanks anyway :)

tirrorex commented Feb 26, 2018

Pretty sure i didn't need it anymore, since ios 11 i found a workaround that suits my usecase better.
Thanks anyway :)

@aysnort

This comment has been minimized.

Show comment
Hide comment
@aysnort

aysnort Aug 21, 2018

By me it didn't work when setting the constraint but this worked
self.playerView.frame.size = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height)

aysnort commented Aug 21, 2018

By me it didn't work when setting the constraint but this worked
self.playerView.frame.size = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment