0

我正在开发一个应用程序,我的应用程序仅支持纵向。

支持的方向:

在此处输入图像描述

现在我在 viewController 中使用 MPMoviePlayerViewController 。现在需要在横向和纵向模式下显示视频。我怎样才能做到这一点。我用于 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];


    }

请给马线索。提前致谢。

4

1 回答 1

0

我相信你需要真正监听来自 UIDevice 的旋转事件来做你正在尝试的事情。我创建了一个应用程序,它有一个锁定为纵向的 UILabel 和一个在设备方向改变时旋转的 MPMoviePlayerViewController。我使用了自动布局(因为这是我最熟悉的),但在 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
于 2013-09-29T08:48:57.037 回答