@ NorthBlast的答案適用於檢測任何出現的UIWindow
在保存UIWebView
的UIViewController
之上。不幸的是,很難過濾什麼樣的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;
}
我發現** ** UIWindowDidBecomeVisibleNotification時被調用視頻開始播放全屏。當視頻消失時調用** UIWindowDidBecomeHiddenNotification **。 – 2015-03-24 17:32:50
@IgorKulagin,這是正確的。 – NorthBlast 2015-03-25 01:55:15
你救了我的夜晚:) – 2015-08-19 13:08:42