2012-12-18 46 views
2

我有一個很奇怪的問題。我想要一個視頻以橫向模式顯示,但我似乎無法使其工作。即使我無法讓它始終顯示風景,至少我希望它表現出色,而且我也無法做到這一點!這裏是我的代碼:如何在iOS 5上以橫向模式進行視頻顯示?

#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來肖像,它看起來像這樣:

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; 
} 

我也嘗試插入行 初始界面的方向=景觀(右home鍵) 在Info.plist中

什麼我越來越如果我在橫向模式下啓動應用程序,如果我將iPad旋轉到縱向,則它保持橫向。大! 但是,如果我以縱向模式啓動應用程序,則視頻將以縱向模式顯示。一旦我將它旋轉到風景,我不能將它旋轉回肖像,這很好,但我不希望它以肖像開始!

EDIT 2

好了,現在這更是怪異。如果我嘗試使用iPhone,它效果很好。無論以橫向還是縱向啓動應用程序,視頻總是以橫向顯示。

但是,如果我嘗試在一個的iPad,在EDIT 1出現...問題:S

任何想法?

謝謝!

回答

4

當您將它作爲子視圖添加時,您是否嘗試過設置MPMoviePlayerViewController的視圖的框架?

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

爲了讓應用程序只在橫向模式下運行,你應該確保你只選擇你的應用程序的plist想要的方向。在Xcode 4中,目標設置中有一個方便的支持界面方向部分,請確保您只在此選擇風景。如果仍然存在問題,則必須確保在視圖堆棧中的所有可見控制器上禁用自動旋轉。

enter image description here

+0

你是一個救星!它修復了播放器顯示問題! :D我仍然有另一個問題,雖然這是問題的標題,我不能讓它始終顯示在橫向模式。看看我的編輯,我進一步描述一下......再次感謝!我想提高你的答案,但我還沒有足夠的聲望。 – Jan

+0

現在請看看編輯2 ...太奇怪了...謝謝! – Jan

+0

耶,我現在可以升職了,所以我只是投了你的答案。現在,如果我只能解決其他問題!謝謝! – Jan

0

shouldAutorotateToInterfaceOrientation被棄用的iOS 6,你是否嘗試過使用supportedInterfaceOrientations

如果你想支持的iOS 5 & 6那麼我相信你需要同時使用:

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

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

我沒有測試此所以你要爲它的價值。

+1

這給我在iPad上的結果與以前相同(請參閱編輯1)。如果我在Landscape中啓動應用程序,那麼一切都很好,我無法旋轉到Portrait。但是,如果我在肖像中啓動應用程序,視頻將顯示爲肖像......這讓我瘋狂! – Jan

相關問題