【问题标题】:How can I make a video display in Landscape mode on iOS 5?如何在 iOS 5 上以横向模式显示视频?
【发布时间】:2012-12-18 23:29:50
【问题描述】:

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

#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)

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

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

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

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

谢谢!

编辑 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 中

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

编辑 2

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

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

有什么想法吗?

谢谢!

【问题讨论】:

    标签: iphone objective-c ipad ios5


    【解决方案1】:

    您是否尝试在将MPMoviePlayerViewControllers 视图添加为子视图时设置它的框架?

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

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

    【讨论】:

    • 你是救生员!!它修复了播放器显示问题! :D 不过,我仍然遇到另一个问题,即问题的标题,我无法让它始终以横向模式显示。看看我的编辑,我在其中进一步描述......再次感谢!我想为你的答案投票,但我还没有足够的声誉。
    • 是的,我现在可以投票了,所以我刚刚投票给你的答案。现在要是我能解决另一个问题就好了!谢谢!
    • Tark,抱歉,如果帖子中不清楚。该应用程序确实支持所有方向。我只希望视频以横向模式显示。这就是为什么我不能使用你所说的选项,因为这会阻止应用程序的其余部分在肖像上工作......
    • 这是一个比较棘手的问题。如果您将控制器推送到导航堆栈上,这将不起作用,如果您纵向推送控制器将纵向显示。让它在纯横向模式下工作的唯一方法是模态显示控制器,这将强制旋转事件。
    • 我明白了......你有我如何做到这一点的例子吗?不过我会开始研究...对我来说很奇怪的是,在 iPhone 上这不会发生,无论我如何启动它总是显示横向。我只是在 iPad 上遇到这个问题。 :S
    【解决方案2】:

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

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

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

    我还没有测试过这个,所以值得一试。

    【讨论】:

    • 这给了我和以前在 iPad 上相同的结果(见 EDIT 1)。如果我以横向启动应用程序,那么一切都很好,我无法旋转到纵向。但是如果我以纵向启动应用程序,视频会显示为纵向...这让我发疯了!
    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2013-03-24
    • 1970-01-01
    相关资源
    最近更新 更多