首页 文章

如何在iOS 5上以横向模式进行视频显示?

提问于
浏览
2

我有一个非常奇怪的问题 . 我希望视频以横向模式显示,但我似乎无法使其正常工作 . 即使我不能让它总是显示风景,至少我希望它显示好,我也不能做到!这是我的代码:

#import "SplashViewController.h"
#import "MainViewController.h"
#import "MediaPlayer/MediaPlayer.h"

@interface SplashViewController ()
@property (nonatomic, retain) NSTimer *timer;
@end

@implementation SplashViewController
@synthesize timer = _timer;

-(BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}

- (id)init
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self = [self initWithNibName:@"SplashViewController_iPhone" bundle:nil];
    } else {
        self = [self initWithNibName:@"SplashViewController_iPad" bundle:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSString *url = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"intro.mp4"];

    playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(movieFinishedCallback:)
    name:MPMoviePlayerPlaybackDidFinishNotification
    object:[playerViewController moviePlayer]];

    [playerViewController shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];

    [self.view addSubview:playerViewController.view];

    //play movie
    MPMoviePlayerController *player = [playerViewController moviePlayer];
    player.scalingMode = MPMovieScalingModeAspectFill;
    [player play];
}

- (void) movieFinishedCallback:(NSNotification*) aNotification {
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter]
    removeObserver:self
    name:MPMoviePlayerPlaybackDidFinishNotification
    object:player];
    [player stop];

    [player.view removeFromSuperview];

    [self loadMainView];
}

- (void)loadMainView 
{
    MainViewController *mainVC = [[MainViewController alloc] init];
    [self.navigationController pushViewController:mainVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

这就是古怪......

如果我用我的iPad在横向模式下启动应用程序,视频显示如下(请注意,顶部的栏比widht短!:O)

http://i.stack.imgur.com/kdPUP.png

如果我然后将iPad旋转到Portrait,它看起来像这样:

http://i.stack.imgur.com/iWK68.png

但是,如果我在我的iPad身上以纵向模式启动应用程序,视频显示如下:

http://i.stack.imgur.com/9NXRY.png

如果我然后将iPad旋转到横向,它看起来像这样:

http://i.stack.imgur.com/pW6OK.png

这太棒了!这个最终图像是我希望视频总是看起来像 . 我有什么想法可能做错了???

谢谢!

EDIT 1

好的,通过@Tark回答,我能够解决播放器显示问题 . 现在它无论我如何启动应用程序都显示正常 . 感谢那!!现在缺少的是始终横向模式 .

我尝试了以下方法:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation==UIInterfaceOrientationLandscapeRight)
        return YES;
    return NO;
}

我还尝试在Info.plist中插入行Initial interface orientation = Landscape(右主页按钮)

我得到的是,如果我在横向模式下启动应用程序,如果我将iPad旋转到纵向,它将保持在横向状态 . 大!但如果我在纵向模式下启动应用程序,视频将以纵向模式显示 . 一旦我将它旋转到Landscape,我就无法将其旋转回Portrait,这很好,但我不希望它以Portrait开头!

EDIT 2

好的,现在这更奇怪了 . 如果我在 iPhone 上尝试它,它的效果很好 . 无论我是在横向还是纵向中启动应用程序,视频都会始终显示在横向中 .

但如果我在 iPad 上尝试,编辑1中的问题就出现了......:S

有任何想法吗?

谢谢!

2 回答

  • 4

    当您将其添加为子视图时,您是否尝试设置 MPMoviePlayerViewController 视图的框架?

    ...
    playerViewController.view.frame = self.view.bounds;
    [self.view addSubview:playerViewController.view];
    ...
    

    要使应用程序仅以横向模式运行,您应确保仅在应用程序plist中选择了所需的方向 . 在Xcode 4中,目标设置中有一个方便的“支持接口方向”部分,请确保您只在此处选择横向 . 如果仍有问题,则必须确保在视图堆栈中的所有可见控制器上禁用自动旋转 .

    enter image description here

  • 0

    shouldAutorotateToInterfaceOrientation自iOS 6起已被弃用,您是否尝试过使用supportedInterfaceOrientations

    如果您正在尝试支持iOS 5和6,那么我相信您需要同时使用这两个:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    

    我没有对此进行测试,因此请考虑它的 Value .

相关问题