UIWebView和Swift:检测视频何时开始播放 [英] UIWebView and Swift: Detect when a video starts playing
问题描述
在Objective-C中,我订阅了 UIWindowDidBecomeVisibleNotification
,以了解某些视图是否超出了我当前的视图控制器,使用:
In Objective-C, I subscribed to the UIWindowDidBecomeVisibleNotification
to know if some view gets above my current view controller, using:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoStartedPlaying:)
name:UIWindowDidBecomeVisibleNotification
object:nil];
到目前为止,非常好。然后,在通知中,我可以检查对象是否不某些类(例如 _UIAlertControllerShimPresenterWindow
-alert views-或 UITextEffectsWindow
-native sharing view-)。在Objective-C中,我这样做:
So far, so good. Then, in the notification, I could check if the object is not of certain classes (like _UIAlertControllerShimPresenterWindow
-alert views- or UITextEffectsWindow
-native sharing view-). In Objective-C, I did it this way:
- (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中做同样的事情,所以我订阅了通知:
This allowed me to stop playing sounds (in this case, an HTTP radio streaming) when starting a video from a UIWebView
(which is used to present news). I've tried to do the same thing in Swift, so I subscribed to the notification:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoStartedPlaying:", name: UIWindowDidBecomeVisibleNotification, object: nil)
现在,收到通知时......
And now, when receiving the notification...
func videoStartedPlaying(notification: NSNotification) {
if <radio_is_playing> && !notification.object? is _UIAlertControllerShimPresenterWindow && !notification.object? is UITextEffectsWindow {
// Stop the radio stream
}
}
Xcode说使用未声明的类型'_UIAlertControllerShimPresenterWindow'
。 UITextEffectsWindow
也会发生同样的事情。
Xcode says Use of undeclared type '_UIAlertControllerShimPresenterWindow'
. The same thing happens with UITextEffectsWindow
.
我认为我必须导入某些东西检测这些类,但我应该导入什么?
I assume that I have to import something to detect those classes, but what should I import?
有没有其他方法可以在没有桥接Objective-C的情况下执行此操作?我怎么能访问那个班级?
Is there any other way to do it without bridging Objective-C? How could I access that class?
提前谢谢你。
推荐答案
您可以比较班级名称而不是班级本身,请参阅这里获取班级名称。
you could compare the class name instead the class itself, refer here to get the class name.
这篇关于UIWebView和Swift:检测视频何时开始播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!