@ câu trả lời NorthBlast của hoạt động tốt để phát hiện bất kỳ UIWindow
nào xuất hiện trên đầu trang của UIViewController
giữ UIWebView
. Thật không may, rất khó để lọc loại UIWindow
là (vì, tốt ... bạn không thể thực sự biết nếu nó là một video hoặc một số loại cửa sổ khác).
Có 3 trường hợp đặc biệt tôi thích để lọc, trong đó bạn chắc chắn họ KHÔNG cửa sổ máy nghe nhạc video, đó là:
1) _UIAlertControllerShimPresenterWindow
, mà là một loại cửa sổ xuất hiện khi sử dụng cảnh báo (như UIAlertView
).
2) UITextEffectsWindow
, xuất hiện khi trình bày các cửa sổ iOS đặc biệt (như cửa sổ chia sẻ, UIActivityViewController
).
3) UIRemoteKeyboardWindow
xuất hiện khi trình bày bàn phím (vì lý do nào đó, lớp này chỉ xuất hiện với tôi khi sử dụng Swift, nhưng trên Objective-C nó không ... không có đầu mối tại sao).
Vì vậy, để đăng ký thông báo, tôi sử dụng (giống như @NorthBlast nói):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidBecomeActive:)
name:UIWindowDidBecomeVisibleNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidBecomeHidden:)
name:UIWindowDidBecomeHiddenNotification
object:nil];
Sau đó, việc triển khai:
- (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;
}
Nguồn
2017-03-16 17:08:26
Tôi đã phát hiện ra rằng ** UIWindowDidBecomeVisibleNotification ** được gọi khi video bắt đầu phát toàn màn hình. Và ** UIWindowDidBecomeHiddenNotification ** được gọi khi video biến mất. –
@IgorKulagin đúng vậy. – NorthBlast
Bạn tiết kiệm đêm của tôi :) –