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 上以横向模式启动应用程序,视频会显示如下(请注意,顶部的栏不要比宽度短!:O)

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

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

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

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

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

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

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

太棒了!这个最终图像是我希望视频始终看起来的样子。有什么想法我可能做错了吗???

谢谢!

编辑 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 (right home button)

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

编辑 2

好吧,现在这更奇怪了。如果我在iPhone上试用,效果很好。无论我以横向还是纵向启动应用程序,视频始终以横向显示。

但是,如果我在iPad上尝试,则会出现 EDIT 1 中的问题......:S

有任何想法吗?

谢谢!

4

2 回答 2

4

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

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

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

在此处输入图像描述

于 2012-12-19T17:11:32.150 回答
0

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

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

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

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

我没有测试过这个,所以把它当作它的价值。

于 2012-12-19T20:20:40.120 回答