我尝试向视频播放器添加一个按钮,当视频从嵌入式Web浏览器内部变为全屏时,该按钮会添加到视图中。
我看到了这个:Detect when a webview video becomes fullscreen on ios8。但是,我认为在那一点上我们没有指向视频播放器的指针。也许有一种方法可以遍历窗口视图的所有子视图并抓取任何AVPlayer实例?
理想情况下,我可以这样做:
NSNotificationCenter.defaultCenter().addObserverForName(
UIWindowDidResignKeyNotification,
object: self.view.window,
queue: nil
) { notification in
let window = whatever the window is now
let player = window.methodThatReturnsVideoPlayer
// do stuff with player
let button = UIButton(...)
window.view.addSubView(button)
}
答案 0 :(得分:1)
没有公共API方法可以在UIWebView
中获取指向视频播放器的指针。
然而,有一种不安全的方式来访问视频播放器。使用反射,您可以遍历UIWebView
的子视图并尝试找到播放器:
- (UIView *)getViewInsideView:(UIView *)view withPrefix:(NSString *)classNamePrefix {
UIView *webBrowserView = nil;
for (UIView *subview in view.subviews) {
if ([NSStringFromClass([subview class]) hasPrefix:classNamePrefix]) {
return subview;
} else {
if((webBrowserView = [self getViewInsideView:subview withPrefix:classNamePrefix])) {
break;
}
}
}
return webBrowserView;
}
- (UIViewController *)movieControllerFromWebView:(UIWebView *)webview {
UIViewController *movieController = nil;
@try {
UIView *movieView = [self getViewInsideView:webview withPrefix:[NSString stringWithFormat:@"MP%@oView", @"Vide"]];
SEL mpavControllerSel = NSSelectorFromString([NSString stringWithFormat:@"mp%@roller", @"avCont"]);
if ([movieView respondsToSelector:mpavControllerSel]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
movieController = (UIViewController *)[movieView performSelector:mpavControllerSel];
#pragma clang diagnostic pop
}
}
@catch (NSException *exception) {
NSLog(@"Failed to get movieController: %@", exception);
}
return movieController;
}
此代码已从YouTubeHacks.m采用。
您的milage可能会有所不同,因为Apple倾向于使用不同的SDK(MPMoviePlayerController
,UIMovieView
,MPVideoView
,UIMoviePlayerController
等更改其播放器对象的名称。 )。我会查看MediaPlayer.framework private headers以供参考。