Skip to content

Commit

Permalink
Implemented allow landscape mode and portrait
Browse files Browse the repository at this point in the history
- Now allows to rotate to landscape mode even if the project is set to
portrait mode.
- Also, when the player is finished it allows to force back to portrait
mode only.
- Still more work to be done and clean up..
  • Loading branch information
JV17 committed Nov 2, 2014
1 parent cf006d4 commit 1c10c10
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 41 deletions.
1 change: 1 addition & 0 deletions AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic) BOOL videoIsInFullscreen;

@end

11 changes: 11 additions & 0 deletions AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,15 @@ - (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if(self.videoIsInFullscreen == YES)
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
return UIInterfaceOrientationMaskPortrait;
}
}

@end
2 changes: 0 additions & 2 deletions Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
2 changes: 1 addition & 1 deletion ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>
#import "YTPlayerView.h"

@interface ViewController : UIViewController
@interface ViewController : UIViewController

@property (nonatomic, strong) YTPlayerView *player;

Expand Down
8 changes: 2 additions & 6 deletions ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ - (void)viewDidLoad {
// Do any additional setup after loading the view, typically from a nib.

self.player = [[YTPlayerView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, 220)];

self.player.autoplay = YES;
self.player.modestbranding = YES;
self.player.listType = @"something";

[self.player allowLandscapeMode];

[self.player allowAutoResizingPlayerFrame];
self.player.allowLandscapeMode = YES;
self.player.allowAutoResizingPlayerFrame = YES;

[self.player loadWithPlaylistId:@"PLEE58C6029A8A6ADE"];

Expand Down
9 changes: 7 additions & 2 deletions YoutubeHelper/YTPlayerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ typedef enum {
@interface YTPlayerView : UIView<NSObject,UIWebViewDelegate>

// for more information visit https://developers.google.com/youtube/player_parameters
@property BOOL allowLandscapeMode;

@property BOOL forceBackToPortraitMode;

@property BOOL allowAutoResizingPlayerFrame;

@property BOOL autohide; // default is 2.. available values 0,1,2 (1 and 2 literraly the same) so if(YES then = 1)

Expand Down Expand Up @@ -699,7 +704,7 @@ typedef enum {
* https://developers.google.com/youtube/iframe_api_reference#getPlaylistIndex
*
*/
- (void)allowLandscapeMode;
//- (void)allowLandscapeMode;

/**
* This method allows to update the youtube webview frame to change to full screen
Expand All @@ -708,6 +713,6 @@ typedef enum {
* https://developers.google.com/youtube/iframe_api_reference#getPlaylistIndex
*
*/
- (void)allowAutoResizingPlayerFrame;
//- (void)allowAutoResizingPlayerFrame;

@end
155 changes: 125 additions & 30 deletions YoutubeHelper/YTPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#import "YTPlayerView.h"
#import "AppDelegate.h"

#define IS_OS_6_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
Expand Down Expand Up @@ -70,6 +71,9 @@ @interface YTPlayerView()

@implementation YTPlayerView

@synthesize allowLandscapeMode = _allowLandscapeMode;
@synthesize forceBackToPortraitMode = _forceBackToPortraitMode;
@synthesize allowAutoResizingPlayerFrame = _allowAutoResizingPlayerFrame;
@synthesize autohide = _autohide;
@synthesize autoplay = _autoplay;
@synthesize cc_load_policy = _cc_load_policy;
Expand Down Expand Up @@ -431,6 +435,22 @@ - (NSArray *)availableQualityLevels

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (self.allowLandscapeMode) {
// allows youtube player in landscape mode
if ([request.URL.absoluteString isEqualToString:@"ytplayer://onStateChange?data=3"])
{
[self playerStarted];

return NO;
}
if ([request.URL.absoluteString isEqualToString:@"ytplayer://onStateChange?data=2"])
{
[self playerEnded];

return NO;
}
}

if ([request.URL.scheme isEqual:@"ytplayer"])
{
[self notifyDelegateOfYouTubeCallbackUrl:request.URL];
Expand Down Expand Up @@ -870,20 +890,20 @@ - (NSString *)stringForJSBoolean:(BOOL)boolValue
* @param ...
* @return void...
*/
- (void)allowLandscapeMode
{
// adding notification center to receive AVPlayer states
if(IS_OS_6_OR_LATER && !IS_OS_8_OR_LATER)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerEnded:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
}
else if (IS_OS_8_OR_LATER)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStarted:) name:UIWindowDidBecomeVisibleNotification object:self.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerEnded:) name:UIWindowDidBecomeHiddenNotification object:self.window];
}
}
//- (void)allowLandscapeMode
//{
// // adding notification center to receive AVPlayer states
// if(IS_OS_6_OR_LATER && !IS_OS_8_OR_LATER)
// {
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStarted) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerEnded) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
// }
// else if (IS_OS_8_OR_LATER)
// {
//// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStarted) name:UIWindowDidBecomeVisibleNotification object:self.window];
//// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerEnded) name:UIWindowDidBecomeHiddenNotification object:self.window];
// }
//}


/**
Expand All @@ -893,16 +913,10 @@ - (void)allowLandscapeMode
* @param ...
* @return void...
*/
- (void)allowAutoResizingPlayerFrame
{
// current device
UIDevice *device = [UIDevice currentDevice];

//Tell it to start monitoring the accelerometer for orientation
[device beginGeneratingDeviceOrientationNotifications];
//Get the notification centre for the app
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];
}
//- (void)allowAutoResizingPlayerFrame
//{
//
//}


/**
Expand All @@ -922,14 +936,32 @@ - (void)dealloc
}
else if (IS_OS_8_OR_LATER)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:nil];
// [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil];
// [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:nil];
}

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}

- (void)webViewDidFinishLoad:(UIWebView*)webView
{
if(self.allowLandscapeMode)
{
// adding listener to webView
[_webView stringByEvaluatingJavaScriptFromString:@" for (var i = 0, videos = document.getElementsByTagName('video'); i < videos.length; i++) {"
@" videos[i].addEventListener('webkitbeginfullscreen', function(){ "
@" window.location = 'videohandler://begin-fullscreen';"
@" }, false);"
@""
@" videos[i].addEventListener('webkitendfullscreen', function(){ "
@" window.location = 'videohandler://end-fullscreen';"
@" }, false);"
@" }"
];
}
}


/**
* Executes when player starts full screen of video player (good for changing app orientation)
Expand All @@ -938,9 +970,11 @@ - (void)dealloc
* @param ...
* @return void...
*/
- (void)playerStarted:(NSNotification*)notification
- (void)playerStarted//:(NSNotification*)notification
{
// do something?
((AppDelegate*)[[UIApplication sharedApplication] delegate]).videoIsInFullscreen = YES;

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}


Expand All @@ -951,11 +985,31 @@ - (void)playerStarted:(NSNotification*)notification
* @param ...
* @return void...
*/
- (void)playerEnded:(NSNotification*)notification
- (void)playerEnded//:(NSNotification*)notification
{
// do something?
if(self.forceBackToPortraitMode == YES)
{
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

((AppDelegate*)[[UIApplication sharedApplication] delegate]).videoIsInFullscreen = NO;

[self supportedInterfaceOrientations];

[self shouldAutorotate:UIInterfaceOrientationPortrait];

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}
}

- (NSInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

/**
* Updates player frame depending on orientation
Expand Down Expand Up @@ -997,6 +1051,7 @@ - (UIWebView *)createNewWebView
_webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
_webView.scrollView.scrollEnabled = NO;
_webView.scrollView.bounces = NO;
_webView.delegate = self;

return _webView;
}
Expand All @@ -1016,6 +1071,46 @@ - (NSMutableDictionary*)setDicParameters

#pragma mark - Customs Setters and Getters

// Custom setters and getters for youtube player parameters
// to be loaded when player loads video.
// These parameters can be set by the user, if they are not
// they won't be loaded to the player because, youtube api
// will use defaults parameters when player created.

-(BOOL)allowLandscapeMode {
return _allowLandscapeMode;
}

-(void)setAllowLandscapeMode:(BOOL)allowLandscapeMode {
_allowLandscapeMode = allowLandscapeMode;
}

-(BOOL)forceBackToPortraitMode {
return _forceBackToPortraitMode;
}

-(void)setForceBackToPortraitMode:(BOOL)forceBackToPortraitMode {
_forceBackToPortraitMode = forceBackToPortraitMode;
}

-(BOOL)allowAutoResizingPlayerFrame {
return _allowAutoResizingPlayerFrame;
}

-(void)setAllowAutoResizingPlayerFrame:(BOOL)allowAutoResizingPlayerFrame {

if(allowAutoResizingPlayerFrame == YES) {
// current device
UIDevice *device = [UIDevice currentDevice];

//Tell it to start monitoring the accelerometer for orientation
[device beginGeneratingDeviceOrientationNotifications];
//Get the notification centre for the app
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];
}
_allowAutoResizingPlayerFrame = allowAutoResizingPlayerFrame;
}

-(BOOL)autohide {
return _autohide;
}
Expand Down

0 comments on commit 1c10c10

Please sign in to comment.