检测webview视频何时在ios8上全屏显示

时间:2014-09-10 13:11:35

标签: ios video uiwebview youtube mpmovieplayercontroller

我有一个应用程序,用户可以从UIWebview打开视频,包括Youtube。 在iOS7中,我能够在开始播放时或在全屏显示时收到通知,这对我向用户显示某些选项和修改界面至关重要。

我以前用过这个:

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

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

但是,从iOS8开始,我无法做到这一点。这就像UIWebview视频不再触发通知一样。但是,正如我所测试的那样,它仍然是从普通视频,非Webview触发的。

知道改变了什么?

5 个答案:

答案 0 :(得分:24)

这是我为此找到的工作..

[[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];

答案 1 :(得分:5)

对于Swift& iOS 9:

sentence_1 = "my first sentence"
sentence_2 = "my second sentence"

ary = [sentence_1, sentence_2]
words = ary.flat_map {|s| s.split}
#=> ["my", "first", "sentence", "my", "second", "sentence"]

答案 2 :(得分:1)

@NorthBlast的答案适用于检测出现在UIWindow UIViewController UIWebView顶部的任何UIWindow。不幸的是,很难过滤出_UIAlertControllerShimPresenterWindow是什么类型的(因为,好吧......你真的不知道它是视频还是其他类型的窗口)。

我希望过滤3个特殊情况,您确定它们是视频播放器窗口,它们是:

1)UIAlertView,这是一种在使用提醒时出现的窗口(如UITextEffectsWindow)。

2)UIActivityViewController,在展示特殊iOS窗口时出现(如共享窗口,UIRemoteKeyboardWindow)。

3)[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeActive:) name:UIWindowDidBecomeVisibleNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeHidden:) name:UIWindowDidBecomeHiddenNotification object:nil]; 在出现键盘时出现(出于某种原因,这个类只在我使用Swift时出现,但是在Objective-C上它没有...不知道为什么会这样)。

因此,为了订阅通知,我使用(就像@NorthBlast说的那样):

- (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;
}

然后实现:

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>      
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

答案 3 :(得分:1)

Swift 4.2,iOS 12.1和WKWebView的更新:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // listen for videos playing in fullscreen
    NotificationCenter.default.addObserver(self, selector: #selector(onDidEnterFullscreen(_:)), name: UIWindow.didBecomeVisibleNotification, object: view.window)

    // listen for videos stopping to play in fullscreen
    NotificationCenter.default.addObserver(self, selector: #selector(onDidLeaveFullscreen(_:)), name: UIWindow.didBecomeHiddenNotification, object: view.window)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // remove video listeners
    NotificationCenter.default.removeObserver(self, name: UIWindow.didBecomeVisibleNotification, object: view.window)
    NotificationCenter.default.removeObserver(self, name: UIWindow.didBecomeHiddenNotification, object: view.window)
}

@objc func onDidEnterFullscreen(_ notification: Notification) {
    print("video is now playing in fullscreen")
}

@objc func onDidLeaveFullscreen(_ notification: Notification) {
    print("video has stopped playing in fullscreen")
}

答案 4 :(得分:0)

对于swift:

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