2014-09-10 32 views
12

我有一個應用程序,用戶可以從UIWebview中打開視頻,其中包括Youtube視頻。 在iOS7中,我開始播放時或者全屏時收到通知,這對於我向用戶顯示某些選項並修改界面至關重要。當webview視頻變成ios8的全屏時檢測到

我用這個:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoExitFullScreen:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoEnterFullScreen:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil]; 

然而,由於iOS8上,我無法做到這一點。這就像通知不再由UIWebview視頻觸發。但是,正如我測試的那樣,它仍然是從正常的視頻,非Web視圖觸發的。

有什麼想法改變了什麼?

回答

19

這是工作,我發現身邊的這個..

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(VideoExitFullScreen:) 
               name:UIWindowDidBecomeVisibleNotification 
               object:self.view.window]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(VideoEnterFullScreen:) 
               name:UIWindowDidBecomeHiddenNotification 
               object:self.view.window]; 
+3

我發現** ** UIWindowDidBecomeVisibleNotification時被調用視頻開始播放全屏。當視頻消失時調用** UIWindowDidBecomeHiddenNotification **。 – 2015-03-24 17:32:50

+0

@IgorKulagin,這是正確的。 – NorthBlast 2015-03-25 01:55:15

+0

你救了我的夜晚:) – 2015-08-19 13:08:42

5

對於斯威夫特&的iOS 9:

NSNotificationCenter.defaultCenter().addObserverForName(
    UIWindowDidResignKeyNotification, 
    object: self.view.window, 
    queue: nil 
) { notification in 
    print("Video is now fullscreen") 
} 


NSNotificationCenter.defaultCenter().addObserverForName(
    UIWindowDidBecomeKeyNotification, 
    object: self.view.window, 
    queue: nil 
) { notification in 
    print("Video stopped") 
} 
0

爲SWIFT:

NotificationCenter.default.addObserver(self, selector: #selector(xxx), name: NSNotification.Name.MPMoviePlayerDidExitFullscreen, object: nil)

+0

這將不再適用於iOS 9等。現在已棄用。 – Tobonaut 2017-02-24 19:53:36

0

@ NorthBlast的答案適用於檢測任何出現的UIWindow在保存UIWebViewUIViewController之上。不幸的是,很難過濾什麼樣的UIWindow(因爲,好吧,你不能真正知道它是一個視頻還是其他類型的窗口)。

有3種特殊情況下,我更喜歡過濾,在你確定他們是不是視頻播放窗口,這些都是:

1)_UIAlertControllerShimPresenterWindow,這是一種窗口使用時出現警報(如UIAlertView)。

2)UITextEffectsWindow,它在呈現特殊iOS窗口(如共享窗口,UIActivityViewController)時出現。

3)UIRemoteKeyboardWindow當出現鍵盤時出現(出於某種原因,這個類只在使用Swift時出現,但在Objective-C上它沒有......不知道爲什麼)。

所以要訂閱通知,我使用(就像@NorthBlast說):

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(windowDidBecomeActive:) 
              name:UIWindowDidBecomeVisibleNotification 
              object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(windowDidBecomeHidden:) 
              name:UIWindowDidBecomeHiddenNotification 
              object:nil]; 

然後實現:

- (void)windowDidBecomeActive:(NSNotification *)notification { 
    if ([self isVideoPlayerWindow:notification.object]) { 
     // Do what's needed if it is a video 
     // For example, on a live streaming radio app, I would stop the audio if a video is started 
    } 
} 

- (void)windowDidBecomeHidden:(NSNotification *)notification { 
    if ([self isVideoPlayerWindow:notification.object]) { 
     // Do what's needed if it is a video 
    } 
} 

- (BOOL)isVideoPlayerWindow:(id)notificationObject { 
    /* 
    Define non video classes here, add more if you need it 
    */ 
    static NSArray *nonVideoClasses = @[ 
     @"_UIAlertControllerShimPresenterWindow", 
     @"UITextEffectsWindow", 
     @"UIRemoteKeyboardWindow" 
    ]; 

    BOOL isVideo = YES; 
    for (NSString *testClass in nonVideoClasses) { 
     isVideo = isVideo && ! [notificationObject isKindOfClass:NSClassFromString(testClass)]; 
    } 

    return isVideo; 
}