14

我有一個支持所有接口方向的iPhone應用程序(iOS6 +)。但是,當MPMoviePlayerController播放全屏視頻時,只應支持橫向。強制整個屏幕上的橫向方向MPMoviePlayerController在退出全屏時阻止了正確的旋轉

我在Stack Overflow上找到了下面的解決方案,它工作。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; 

...

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { 
    if (self.landscapeOnlyOrientation) { 
     return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; 
    } 
    return UIInterfaceOrientationMaskAll; 
} 

- (void)moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification { 
    self.landscapeOnlyOrientation = YES; 
} 

- (void)moviePlayerWillExitFullscreenNotification:(NSNotification*)notification { 
    self.landscapeOnlyOrientation = NO; 
} 

然而,一個惱人的問題仍然存在,即如果視頻中退出縱向全屏(強制橫向播放後),基本觀點不旋轉回。我必須手動將設備旋轉到風景並返回到肖像以觸發更新方向。有什麼辦法可以通過編程方式觸發這種更新?

下面的一組截圖應該說明我的意思:

enter image description here enter image description here enter image description here

注:由於種種原因,使用MPMoviePlayerViewController是不可能的。

+1

我幾個月前提交的bug蘋果在這個問題上。我建議你這樣做。問題是沒有諮詢底層視圖控制器的方向方法。 – matt

+0

任何有關解決方法的建議? – svth

+0

不可以。您可以嘗試阻止使用全屏模式。或者不要使用MPMoviePlayerController。基本上,這只是蘋果公司的一個大不相干的事情,開發人員需要在他們修復之前一直保持這種狀態。 – matt

回答

11

大家好我有我解決了這同樣的問題 -

這裏是我完整的代碼....

您需要首先改變的appdelegate:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
if ([[[NowPlaying sharedManager] playerViewController] allowRotation])//Place your condition here 
{ 
    return UIInterfaceOrientationMaskAll; 
} 
return UIInterfaceOrientationMaskPortrait; 
} 

註冊通知的全屏控制:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) 
              name:MPMoviePlayerWillEnterFullscreenNotification 
              object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) 
              name:MPMoviePlayerWillExitFullscreenNotification 
              object:nil]; 

然後在播放器中添加的代碼行:

- (void)moviePlayerWillEnterFullscreenNotification:(NSNotification *)notification 
{ 
dispatch_async(dispatch_get_main_queue(),^
       { 
        self.allowRotation = YES; 
       }); 
} 



- (void)moviePlayerWillExitFullscreenNotification:(NSNotification *)notification 
{ 
self.allowRotation = NO; 
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone]; 

dispatch_async(dispatch_get_main_queue(),^
       { 

        //Managing GUI in pause condition 
         if (self.currentContent.contentType == TypeVideo && self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused) 
        { 
         [self.moviePlayerController pause]; 
         if (self.playButton.selected) 
          self.playButton.selected = NO; 
        } 
        self.view.transform = CGAffineTransformMakeRotation(0); 
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; 
        self.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); 
       }); 
} 

此代碼已在iOS6和iOS7中正常工作。謝謝:)

請讓我知道,如果有任何問題.....

+0

什麼條件我必須在這裏檢查?if([ [[NowPlaying sharedManager] playerViewController] allowRotation])//將您的條件放在這裏以及現在播放的內容是什麼? – karthikeyan

+0

嗨Vijay,感謝您的代碼。在刪除WillExitFullScreen方法中的dispatch_async代碼後它工作正常。你能告訴我爲什麼你把代碼沒用,什麼是TypeVideo? – Satya

+1

感謝Satya發表評論。 我用dispatch_async來訪問主線程,因爲在我的情況下,當我進入橫向模式時,我以前的GUI在動畫時變得扭曲。 在我的情況下,我在同一個播放器上播放音頻和視頻,並且每當一個視頻開始播放時,它將獲得全屏而不是音頻歌曲。所以我用TypeVideo來播放視頻歌曲。 – Kumar

0

這可能聽起來很瘋狂,但是,您可以嘗試在打開視頻視圖控制器之前在本地保存最後的方向,然後使用application:supportedInterfaceOrientationsForWindow:返回保存的方向並強制視圖控制器停留在其上而不旋轉。

2

你可以嘗試以「力」的方向刷新讓系統正確的方向你:

- (void)forceOrientationRefresh 
{ 
    // Force orientation refresh 
    [self presentModalViewController:[UIViewController new] 
          animated:NO]; 
    [self dismissModalViewControllerAnimated:NO]; 
} 

這是黑客十歲上下,但它的工作原理。

0

您可以通過編程改變你的方向是這樣

-(void)viewDidAppear:(BOOL)animated 
{ 

    if(UIDeviceOrientationIsPortrait(self.interfaceOrientation)){ 
     if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) 
     { 
      objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft); 

     } 
    } 

} 

而且,別忘了添加#import <objc/message.h>

5

您需要繼承,並提供景觀爲支持的接口方向。

@interface MyMovieViewController : MPMoviePlayerViewController 
@end 

@implementation MyMovieViewController 

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

- (UIInterfaceOrientationMask)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

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

@end 
+0

這應該是接受的答案 – yonix

0

我認爲你可以註冊你的viewcontroller設備的方向和強制調用viewcontroller的方向的方法。

-1

你需要添加此代碼爲iOS7 它完美的作品,簡單

-(NSUInteger)supportedInterfaceOrientations { 

    return UIInterfaceOrientationMaskPortrait; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationPortrait; 
} 

.... creating a player 
MPMoviePlayerViewController *mp =[[MPMoviePlayerViewController alloc] initWithContentURL:url]; 
...make settings and present it 
[self presentMoviePlayerViewControllerAnimated:mp]; 
0

您可以使用supportedIterfaceOrientationsForWindow然後尋找: MPInlineVideoFullscreenViewController。找到正確的視圖控制器是有點棘​​手,但它的工作原理。

下面是示例代碼:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ 
    if ([NSStringFromClass([self.window.rootViewController.presentedViewController.presentedViewController class]) isEqualToString:@"MPInlineVideoFullscreenViewController"]){ 
    return UIInterfaceOrientationMaskAllButUpsideDown; 
} 
    return UIInterfaceOrientationMaskLandscape; 
} 
相關問題