raed
raed

Reputation: 5075

Play YouTube Video in fullscreen and allow rotation only for the video for iOS7 and iOS8

I m trying to play a YouTube video on UIWebView as bellow :

// Create the URL
_videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/watch?v=%@", _videoID]];

// Create the request with the URL
NSURLRequest *requestObj = [NSURLRequest requestWithURL:_videoUrl];

// Load the request into the Web View
[_webView loadRequest:requestObj];

The youtube page shows, when I clique on the video it starts to play, but it didn't rotate.

I spent on week now looking for different solution, by implementing "shouldAutorotate" and "supportedInterfaceOrientations", without success !

The last thing I tried is to add a listener if the video is playing in fullscreen mode, in AppDelegate.m I added to "didFinishLaunchingWithOptions" the code bellow :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

And implemented :

- (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
self.allowRotation = YES; }

- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
self.allowRotation = NO; }

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.forceLandscapeRight) {
    return UIInterfaceOrientationMaskLandscapeRight;
}
if (self.allowRotation) {
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

return UIInterfaceOrientationMaskPortrait; }

The problem is that neither "moviePlayerWillEnterFullscreenNotification" or "moviePlayerWillExitFullscreenNotification" are called.

Help please!

Upvotes: 4

Views: 1762

Answers (3)

alcarv
alcarv

Reputation: 899

This is my solution in Swift to the problem. It's based on the answers above but simpler:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
  // if it's our window, portrait. Other windows are (probably) the fullscreen video player ;)
  if self.window == window {
    return .Portrait
  } else {
    return [.Portrait, .Landscape]
  }
}

Upvotes: 3

raed
raed

Reputation: 5075

I just did some researchs and I came up with the solution to support both iOS7 and iOS8. To allow the rotation of the application only when the YouTube video is played, here're the steps to follow :

  1. AppDelegate.m

Import #import <MediaPlayer/MediaPlayer.h> And Implement the function "supportedInterfaceOrientationsForWindow" as bellow :

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

if ([[window.rootViewController presentedViewController]
     isKindOfClass:[MPMoviePlayerViewController class]] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {

    return UIInterfaceOrientationMaskAllButUpsideDown;
}else {

    if ([[window.rootViewController presentedViewController]
         isKindOfClass:[UINavigationController class]]) {

        // look for it inside UINavigationController
        UINavigationController *nc = (UINavigationController *)[window.rootViewController presentedViewController];

        // is at the top?
        if ([nc.topViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
            return UIInterfaceOrientationMaskAllButUpsideDown;

            // or it's presented from the top?
        } else if ([[nc.topViewController presentedViewController]
                    isKindOfClass:[MPMoviePlayerViewController class]]) {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
}

return UIInterfaceOrientationMaskPortrait;
}
  1. ViewController.m

On your "ViewController.m", Add the following code to "ViewDidLoad", this code Allow you to create a listener if the video is playing in fullscreen mode :

- (void)viewDidLoad
{
    [super viewDidLoad];

    // SetUp notifications when video gets played and stopped
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];

    // Init The Video WebView
    [self initVideoWebView];
}

This Function allows you to initiate your WebView with an YouTube URL :

- (void) initVideoWebView {

    // Create the URL
    _videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/watch?v=%@", _videoID]];

    // Create the request with the URL
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:_videoUrl];

    // Load the request into the Web View
    [_webView loadRequest:requestObj];
}

The Last thing to do is to implements the functions bellow on your ViewControler :

-(BOOL) shouldAutorotate {
 return NO; }

-(NSUInteger)supportedInterfaceOrientations{
 return UIInterfaceOrientationMaskPortrait; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
 return UIInterfaceOrientationPortrait; }

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait; }

If this helped, up rate :)

Upvotes: 1

raed
raed

Reputation: 5075

Found an answer :

I had to use

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];

Instead of

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

And implement the methodes in my ViewControler

-(BOOL) shouldAutorotate {
 return NO; }

-(NSUInteger)supportedInterfaceOrientations{
 return UIInterfaceOrientationMaskPortrait; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
 return UIInterfaceOrientationPortrait; }

For more details : iOS 6.0+ autorotation for youtube embedded video

Hope this will help :)

Upvotes: 2

Related Questions