2012-05-21 136 views
22

我有一些奇怪的行爲,其中MPMoviePlayerViewController不是auto - 當orientation更改時發生旋轉。不過,我在新項目中重新創建了相同的視圖層次結構,並且當播放器啓動時,它會旋轉到各個方向。我已經研究過這個項目,尋找可能明確設定方向的任何東西,但沒有任何東西。MPMoviePlayerViewController不會自動旋轉

我會在此列出所有相關信息以及迄今爲止嘗試過的內容。

視圖層次目前看起來是這樣的:

  • 導航控制器
  • 「根」 視圖控制器 < - 導航控制器的 'RootViewController的'
  • 「飼料」 - 視圖 - 控制器 < - 由Root推送到導航棧上VC
  • 「預覽」視圖控制器 < - 從進料
  • MPMoviePlayerViewController子類 <呈現爲模態VC - 通過進給VC經由「presentMoviePlayerViewControllerAnimated」

每個類視圖層次結構呈現響應應該只對UIInterfaceOrientationPortrait使用YESAutorotateToInterfaceOrientation。

事情我已經嘗試:

  • 手動從「根」 VC發送shouldAutorotateToInterfaceOrientation高達堆棧到MPMoviePlayerViewController
  • 重寫MPMoviePlayerViewController子實施shouldAutorotateToInterfaceOrientation返回YES針對橫向方向適用於所有方向。
  • 在項目摘要選項卡中設置「支持的設備方向」。
  • 調用其他的風險投資,如飼料中presentMoviePlayerViewControllerAnimated VC

如果電影播放器​​在同一視圖層次新鮮的項目,有什麼事情可以在路上及彼正常旋轉。任何想法可能會卡住方向?

+5

使用一個UINavigationController(或的UITabBarController)申請僅當旋轉**所有**可顯示(堆疊/標籤)viewControllers同意旋轉。在你的情況下,確保當在'shouldAutorotateToInterfaceOrientation'中被詢問時,MPMoviePlayerViewController子類實際上在所有情況下返回'YES'。 – Till

+0

@Till我當然明白,但正如我所說的:在具有相同視圖層次結構的測試項目中,所有視圖在一個旋轉(人像)上達成一致,電影播放器​​旋轉到每個方向都沒有問題。 編輯:你的迴應的後半部分在我的問題的「我嘗試過的東西」下回答。謝謝! – dannyzlo

+1

對,請檢查我編輯的評論,因爲我最初沒有完全理解你的UI層次。 – Till

回答

1

解決方案:

對於任何人都可能會遇到這樣的視頻是不旋轉的原因是,我是不小心加入RootViewController的有窗口的RootViewController的,而不是UINavigationController的。

self.window.rootViewController = navController; 

是正確的

self.window.rootViewController = rootViewController;

謝謝你們的幫助和投入沿途。

3

我發現MPMoviePlayerViewController對象將支持項目的Info.plist設置以支持接口方向。在我的一個項目中,我只允許該文件中的橫向視圖,因此電影播放器​​不會旋轉,即使它在shouldAutorotateToInterfaceOrientation:中回答YES以橫向取向。

編輯:好吧,抓住吸管:你在UIViewController的任何一個子類中都執行automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers嗎?如果是,並且它返回NO,則您的子類必須在方向更改時將適當的方法轉發給任何子控制器。

否則有什麼方法可以看到你的代碼?

+0

謝謝,但不幸的是,這並沒有爲我做。我嘗試設置project-info.plist文件以接受每個方向,以及僅風景,但無濟於事。 – dannyzlo

0

你在使用故事板嗎?比較您的UIViewControllers和您的UINavigationController在破損的項目和測試項目之間的方向設置。屬性檢查器上的「方向」設置可能會將您鎖定到一個方向。

你提到shouldAutorotateToInterfaceOrientation:和你的plist設置,所以我不會進入這...

+0

感謝您的回答,但在這個項目中沒有故事板。 – dannyzlo

7

我會建議你不要使用presentMoviePlayerViewControllerAnimated,而不是添加爲子視圖。我認爲它會很好地解決你的問題。

MPMoviePlayerViewController *mpviewController = [[MPMoviePlayerViewController alloc] 
        initWithContentURL:[NSURL fileURLWithPath:self.filePath]]; 
[self.view addSubview:mpviewController.view]; 
[self setWantsFullScreenLayout:YES]; 

,並清除mpviewController.viewMPMoviePlayerPlaybackDidFinishNotification檢測。讓我看看你的成功...

+0

+1思維不同 –

+0

好想法,但這並沒有奏效:-(。稍後我會重新創建測試項目,不僅僅是使用相同的視圖層次結構,而是使用與生產中相同的類,它會保持更新 - 謝謝! – dannyzlo

+0

最佳答案!!! –

2

我知道這可能是一個愚蠢的建議,但要確保MPMoviePlayerViewController子類中的shouldAutorotateToInterfaceOrientation方法被調用。也許出事了那裏......

還要確保你沒有2子視圖添加到主UIWindow爲指定here

問:爲什麼我的UIViewController中與旋轉設備?

[...]

  • 視圖控制器的UIView的屬性被嵌入內部的UIWindow但 旁邊附加的視圖控制器。

我想這可能也會給你一些問題。您可以在上面的鏈接中找到有關可能出錯的更多信息。

0

有在查詢背後的一些原因..

***要調用MPMoviePlayerViewController ..上"Feed" View Controller所以申請AutoOrientation並嘗試通過PushViewController調用..

***使用MPMoviePlayerController代替的MPMoviePlayerViewController並以FeedViewController添加子視圖..

示例代碼MPMoviePlayerController--

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"type"]]; 
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
videoPlayer.controlStyle = MPMovieControlStyleNone; 
videoPlayer.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.height, self.view.frame.size.width); 
[videoPlayer prepareToPlay]; 
videoPlayer.view.center = self.view.center; 
videoPlayer.fullscreen = YES; 
[self.view addSubview:videoPlayer.view]; 
[videoPlayer play]; 

***檢查您的Xcode目標設置和應用使所有方向..

+0

如果您在viewDidLoad中使用MPMoviePlayerController,請嘗試在viewWillAppear中使用。 –

0

你應該試試這個(成功):在.h文件中

聲明:本

BOOL landscape; 

。 m文件:

-(IBAction)PlayMovie:(NSString *)movieName { 
    landscape = YES; 
    NSBundle *bundle = [NSBundle mainBundle]; 
    NSString *moviePath = [bundle pathForResource:movieName ofType:@"mp4"]; 
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath]; 
    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
    theMovie.scalingMode = MPMovieScalingModeAspectFill; 
    [theMovie play]; 
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL]; 
    [self presentMoviePlayerViewControllerAnimated:moviePlayer]; 
} 

-(void)dismissMoviePlayerViewControllerAnimated { 
    landscape = NO; 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// You can change the return for your needs. 
    if (landscape == YES) { 
     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
    } else { return NO; } 
} 

我在這裏所做的是創建我的電影視圖並將「landscape」BOOL設置爲YES。

然後,「shouldAutorotateToInterfaceOrientation」將檢測到這個並自動控制你的視圖。

而當電影完成後,我將「風景」設置爲NO,以便視圖旋轉回來。

1

幾個想法:

  • 是在UINavigationController設置爲您的應用程序的UIWindowrootViewController財產?你沒有提到這一點。您應該這樣做,而不是將導航控制器的視圖添加到窗口。
  • 如果您一次構建整個層次結構,請嘗試將其分解。您可以在每個階段添加一個按鈕,將下一個視圖控制器添加到層次結構中。
  • 嘗試從視圖控制器層次結構中刪除任何動畫。同時做多個動畫可能會造成麻煩。例如,不允許將UINavigationController中的兩個視圖控制器依次推送到animated:YES。你可能有類似的問題。
  • 確保您在主線程上構建了整個視圖控制器層次結構。
  • 確保沒有其他視圖控制器「負責」旋轉(@MihaiFratu寫道 - 這是常見的旋轉問題的原因,我不得不重複一遍:-))。
0

我有類似的問題。

要解決:

  1. 使所有方位支持的界面取向(目標>摘要)
  2. 現在您的應用程序將開始在所有方向旋轉,如果你不想這樣,則跳過步驟1, (UIApplication的*)應用supportedInterfaceOrientationsForWindow:(一個UIWindow *)窗口{ 返回UIInterfaceOrien僅在的appDelegate

    • (NSUInteger)應用程序中添加以下的方法tationMaskAll; }
  3. 除去各處shouldAutorotateToInterfaceOrientation

  4. 添加下面的方法來用於支持ü所需應用程式

    • (NSUInteger)supportedInterfaceOrientations { 返回UIInterfaceOrientationMaskPortrait取向查看控制器; }
  5. 您可以在MPMoviePlayerViewController的子類無論取向你所需要的視頻播放器

0

本指南幫我檢查了一些步驟我失蹤了,爲了增加同樣的方法,在第4步允許景觀來看待只有視頻播放器,讓應用程序的其餘部分將被固定在豎屏模式:

iOS6 and autorotation tip

0

我有很多痛苦與MPMoviePlayerViewController,應該只有控制器能夠從肖像旋轉到風景,反之亦然。這是iOS7,與故事板的iOS8應用程序。

這裏是解決方案:

  1. 應用應該能執行所有可能需要的取向As on the image
  2. 每個UIViewController的需要只支持肖像模式,應該實現下一個方法,像這樣

    -(BOOL)shouldAutorotate 
    { 
        return YES; 
    } 
    
    -(NSUInteger)supportedInterfaceOrientations 
    { 
        return UIInterfaceOrientationMaskPortrait; 
    } 
    
  3. MPMoviePlayerViewController應該被擴展,下一個方法應該像這樣被覆蓋

    -(BOOL)shouldAutorotate 
    { 
        return YES; 
    } 
    
    -(NSUInteger)supportedInterfaceOrientations 
    { 
        return UIInterfaceOrientationMaskAllButUpsideDown; 
    } 
    
  4. 使用presentMoviePlayerViewControllerAnimated顯示MPMoviePlayerViewController

0

在AppDelegate中添加此方法。米

- (NSUInteger)應用:(UIApplication的*)應用supportedInterfaceOrientationsForWindow:(一個UIWindow *)窗口 {

if(!ISIPAD) 

    { 

    if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]] && ![[self.window.rootViewController presentedViewController] isBeingDismissed]) 

    { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } 
    else 
    { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 
return UIInterfaceOrientationMaskAllButUpsideDown ; 

}