How do you prevent fullscreen video rotation in iO

2019-04-16 11:59发布

问题:

I have an app whose rotation configuration is defined dynamically, therefore I cannot set the supported rotations in the project file.

Therefore I have to handle the new shouldRotate methods (thanks Apple!!)

But despite having overriden these and prevented the full UI from displaying in anything but portrait. When a video view is displayed fullscreen and rotated. The video will rotate to landscape still.

Is there another way to preview the video specifically from rotating at all?

回答1:

Here's an implementation using a MPMoviePlayerViewController subclass supporting portrait and portrait upside down (but you can easily change the mask). This works in iOS 6 as you suggested, prior versions have different rotation selectors.

Usage:

NSURL* url = [NSURL URLWithString:@"your_movie"];
MovieViewController* mvc = [[MovieViewController alloc] initWithContentURL:url];

// I did this from the app delegate. 
// You could push this view controller, present it modally, etc.

[self.window setRootViewController:mvc];

In MovieViewController.h:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface MovieViewController : MPMoviePlayerViewController

- (id)initWithContentURL: (NSURL*) url;

@end

In MovieViewController.m:

#import "MovieViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface MovieViewController ()

@end

@implementation MovieViewController

- (id)initWithContentURL: (NSURL*) url
{
    self = [super initWithContentURL: url];
    if (self) {
        // Custom initialization


    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
    ROTATION CODE
 */
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}


@end

Result (note rotation is enabled on the project for all rotations..):



标签: ios ios6