0

i am working on an application and my app support only portrait orientation.

Supported orientation :

enter image description here

Now i am using a MPMoviePlayerViewController inside a viewController . Now there is a need to display Video both in landscape and portrait mode. How can i achieve this. Code which i am using for MPMovieViewController is :

    -(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];


    }

Please give m a clue. Thanks in advance.

nitin tyagi
  • 1,176
  • 1
  • 19
  • 52

1 Answers1

0

I believe you need to actually listen for the rotation events from the UIDevice to do what you're attempting. I've created an app that has a UILabel that's locked to portrait orientation and a MPMoviePlayerViewController that gets rotated when the device orientation changes. I used autolayout (since that's what I'm most comfortable with), but it should be easy in iOS5-style autoresizing, etc.

Run this at let me know if it's what you're after.

Here's my whole app in 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
ksimons
  • 3,797
  • 17
  • 17
  • i have tried this . but it makes all ViewController in portrait and landscape mode but i want to rotate only MPMovieViewController. – nitin tyagi Sep 29 '13 at 08:57
  • Ok, I think I get what you're after now. I've updated the answer to do as you say – ksimons Sep 29 '13 at 12:01