UIWebView 和 Swift:检测视频何时开始播放

UIWebView and Swift: Detect when a video starts playing

在 Objective-C 中,我订阅了 UIWindowDidBecomeVisibleNotification 以了解是否某些视图超出了我当前的视图控制器,使用:

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

到目前为止,还不错。然后,在通知中,我可以检查对象是否是某些 classes 的 not(例如 _UIAlertControllerShimPresenterWindow -alert views- 或 UITextEffectsWindow -本机共享视图-)。在Objective-C中,我是这样做的:

- (void)videoStartedPlaying:(NSNotification *)notification
{
    if (
        <radio_is_playing>
        &&
        ! [notification.object isKindOfClass:NSClassFromString(@"_UIAlertControllerShimPresenterWindow")] // Alert view
        &&
        ! [notification.object isKindOfClass:NSClassFromString(@"UITextEffectsWindow") ] // Share
        )
    {
        // Video, stop the radio stream
    }
}

这允许我在从 UIWebView(用于显示新闻)开始播放视频时停止播放声音(在本例中为 HTTP 广播流)。我已经尝试在 Swift 中做同样的事情,所以我订阅了通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoStartedPlaying:", name: UIWindowDidBecomeVisibleNotification, object: nil)

现在,当收到通知时...

func videoStartedPlaying(notification: NSNotification) {
    if <radio_is_playing> && !notification.object? is _UIAlertControllerShimPresenterWindow && !notification.object? is UITextEffectsWindow {
        // Stop the radio stream
    }
}

Xcode 表示 Use of undeclared type '_UIAlertControllerShimPresenterWindow'UITextEffectsWindow.

也会发生同样的事情

我假设我必须导入 某些东西 来检测那些 class,但是我应该导入什么?

有没有其他不桥接的方法Objective-C?我怎样才能访问那个 class?

提前谢谢你。

您可以比较 class 名称而不是 class 本身,参考 here 以获得 class 名称。