2013-09-29 2 views
0

Я работаю над приложением, и мое приложение поддерживает только портретную ориентацию.MPMoviePlayerViewController во всех ориентациях

Поддерживаемые ориентации:

enter image description here

Теперь я использую MPMoviePlayerViewController внутри ViewController. Теперь необходимо отображать видео как в альбомном, так и в портретном режимах. Как я могу это достичь. Код, который я использую для MPMovieViewController, составляет:

-(void) playVideo 
{ 
    movieplayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"abc" ofType:@"mp4"]]]; 

    [[NSNotificationCenter defaultCenter] removeObserver:movieplayer 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:movieplayer.moviePlayer]; 

    // Register this class as an observer instead 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(movieFinishedCallback:) 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:movieplayer.moviePlayer]; 

    // Set the modal transition style of your choice 
    movieplayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 

    movieplayer.moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 

    for(UIView* subV in movieplayer.moviePlayer.view.subviews) { 
     subV.backgroundColor = [UIColor clearColor]; 
    } 


    [[movieplayer view] setBounds:CGRectMake(0, 0, 480, 320)]; 
    CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI/2); 
    movieplayer.view.transform = transform; 




    movieplayer.moviePlayer.fullscreen=YES; 
    [self presentModalViewController:movieplayer animated:NO]; 
    self.view addSubview:movieplayer.view]; 

    [movieplayer.moviePlayer play]; 

} 

- (void)movieFinishedCallback:(NSNotification*)aNotification 
{ 
    NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; 


      MPMoviePlayerController *moviePlayer = [aNotification object]; 

     moviePlayer.fullscreen = NO; 
     [movieplayer dismissModalViewControllerAnimated:NO]; 

     // Remove this class from the observers 
     [[NSNotificationCenter defaultCenter] removeObserver:self 
                 name:MPMoviePlayerPlaybackDidFinishNotification 
                 object:moviePlayer]; 


    } 

Просьба указать ключ. Заранее спасибо.

ответ

0

Я считаю, что вам нужно действительно слушать события вращения из UIDevice, чтобы делать то, что вы пытаетесь сделать. Я создал приложение, которое имеет UILabel, который заблокирован для портретной ориентации, и MPMoviePlayerViewController, который поворачивается при изменении ориентации устройства. Я использовал autolayout (так как это то, что мне больше всего нравится), но это должно быть легко в автосохранении в стиле iOS5 и т. Д.

Запустите это, сообщите мне, если это то, что вам нужно.

Вот все мое приложение в AppDelegate.m:

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

@interface MyViewController : UIViewController 
@property (nonatomic, strong) MPMoviePlayerController *player; 
@end 

@implementation MyViewController 

- (void)loadView 
{ 
    [super loadView]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

    self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"]]; 
    self.player.scalingMode = MPMovieScalingModeAspectFit; 
    [self.player play]; 

    UIView *playerView = self.player.view; 

    [self.view addSubview:playerView]; 
    playerView.translatesAutoresizingMaskIntoConstraints = NO; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[playerView]|" 
                     options:0 
                     metrics:nil 
                     views:NSDictionaryOfVariableBindings(playerView)]]; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[playerView]|" 
                     options:0 
                     metrics:nil 
                     views:NSDictionaryOfVariableBindings(playerView)]]; 

    UILabel *wontRotate = [[UILabel alloc] init]; 
    wontRotate.backgroundColor = [UIColor clearColor]; 
    wontRotate.textColor = [UIColor whiteColor]; 
    wontRotate.text = @"This stays in portrait"; 
    wontRotate.translatesAutoresizingMaskIntoConstraints = NO; 

    [self.view addSubview:wontRotate]; 
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:wontRotate 
                  attribute:NSLayoutAttributeCenterX 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:self.view 
                  attribute:NSLayoutAttributeCenterX 
                 multiplier:1.0 
                  constant:0.0]]; 
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:wontRotate 
                  attribute:NSLayoutAttributeCenterY 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:self.view 
                  attribute:NSLayoutAttributeCenterY 
                 multiplier:1.0 
                  constant:0.0]]; 
} 

- (void)deviceOrientationDidChange:(NSNotification *)notification{ 

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    switch (orientation) { 
     case UIDeviceOrientationLandscapeLeft: 
      self.player.view.transform = CGAffineTransformMakeRotation(M_PI/2); 
      break; 
     case UIDeviceOrientationLandscapeRight: 
      self.player.view.transform = CGAffineTransformMakeRotation(3 * M_PI/2); 
      break; 
     case UIDeviceOrientationPortraitUpsideDown: 
      self.player.view.transform = CGAffineTransformMakeRotation(M_PI); 
      break; 
     case UIDeviceOrientationPortrait: 
      self.player.view.transform = CGAffineTransformMakeRotation(2 * M_PI); 
      break; 
     default: 
      NSLog(@"Unknown orientation: %d", orientation); 
    } 
} 


- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 


@end 

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.window.rootViewController = [[MyViewController alloc] init]; 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 


@end 
+0

я попытался это. но он делает все ViewController в портретном и альбомном режимах, но я хочу повернуть только MPMovieViewController. –

+0

Хорошо, я думаю, что теперь я получаю то, что вы делаете. Я обновил ответ, чтобы сделать, как вы говорите – ksimons

Смежные вопросы