Play YouTube video in full screen and allow rotation only for videos for iOS7 and iOS8

I am trying to play a YouTube video in a UIWebView as shown below:

// 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 is displayed when I click on the video that it starts to play, but it does not rotate.

I spent a week now searching for another solution using "shouldAutorotate" and "supportedInterfaceOrientations" without success!

The last thing I tried was to add a listener if the video is playing in full screen mode in AppDelegate.m. I added the code below to "didFinishLaunchingWithOptions":

[[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 "moviePlayerWillEnterFullscreenNotification" or "moviePlayerWillExitFullscreenNotification" are called.

Help me please!

+4
3

:

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

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

ViewControler

-(BOOL) shouldAutorotate {
 return NO; }

-(NSUInteger)supportedInterfaceOrientations{
 return UIInterfaceOrientationMaskPortrait; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
 return UIInterfaceOrientationPortrait; }

: iOS 6.0+ youtube

, :)

+2

Swift . , :

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

, iOS7 iOS8. YouTube, :

  • AppDelegate.m

#import <MediaPlayer/MediaPlayer.h> "supportedInterfaceOrientationsForWindow" :

- (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 presented from the top?
        } else if ([[nc.topViewController presentedViewController]
                    isKindOfClass:[MPMoviePlayerViewController class]]) {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
}

return UIInterfaceOrientationMaskPortrait;
}
  1. ViewController.m

"ViewController.m" "ViewDidLoad", , :

- (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 feature allows you to trigger your WebView with a 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 implement the functions below on your ViewControler:

-(BOOL) shouldAutorotate {
 return NO; }

-(NSUInteger)supportedInterfaceOrientations{
 return UIInterfaceOrientationMaskPortrait; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
 return UIInterfaceOrientationPortrait; }

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait; }

If that helps, speed up :)

+1
source

Source: https://habr.com/ru/post/1541778/


All Articles