2016-04-19 17 views
0

ビデオが埋め込まれたWebブラウザの内部からフルスクリーンになったときに、ビューに追加されるボタンをビデオプレーヤーに追加しようとしています。UIWebViewでフルスクリーン表示にするときにビデオをインターセプトする方法は?

私はこれを見ました: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) 
} 

答えて

1

UIWebViewでビデオプレーヤーへのポインタを取得するためには公共のAPIメソッドがありません:

理想的には私はこのような何かを行うことができます。

ただし、ビデオプレーヤーにアクセスするのは安全ではありません。リフレクションを使用して、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から採用されています。 Appleが異なるのSDK(MPMoviePlayerControllerUIMovieViewMPVideoViewUIMoviePlayerController、など)と、その選手オブジェクトの名前を変更する傾向があるよう

あなたの走行距離は異なる場合があります。私は参考としてMediaPlayer.framework private headersを見るだろう。

+0

ボタンを全画面ビデオプレーヤーに追加する方が簡単でしょうか? – Edmund

+0

@エドモンドどのようにこれをしますか?フルスクリーンプレーヤーの実装はAppleにはプライベートなので、基本的な実装を変更せずにすべてのプレーヤーに魔法のように "ボタンを追加"する方法はありません。 – JAL

+0

私は 'UIWindowDidResignKeyNotification'イベントを使用して、そのウィンドウのコントローラのビューをループし、' addSubview(button) 'を使用していましたが、現在の' window 'のビューを見つける方法がわかりません。 – Edmund

関連する問題