捕获全屏Youtube iFrame的全屏事件并以编程方式执行全屏

时间:2015-03-16 13:04:35

标签: ios objective-c uiwebview youtube-api

我将带有iFrame API的YouTube iFrame嵌入到UIWebView中以播放视频。这是HTML脚本:

<!DOCTYPE html>
<html>
<head>
    <style>
        body { margin: 0; width:100%; height:100%; }
        html { width:100%; height:100%; }
    </style>
</head>
<body>
    <div id="player"></div>

    <script type='text/javascript' src="https://www.youtube.com/iframe_api" ></script>
    <script type='text/javascript'>
    var player;
    YT.ready(function() {
      player = new YT.Player('player', {
  "playerVars" : {
    "enablejsapi" : 1,
    "modestbranding" : 1,
    "autoplay" : 1,
    "showinfo" : 1,
    "controls" : 1,
    "playsinline" : 1,
    "autohide" : 0
  },
  "width" : "100%",
  "videoId" : %@,
  "events" : {
    "onPlaybackQualityChange" : "onPlaybackQualityChange",
    "onStateChange" : "onStateChange",
    "onError" : "onPlayerError",
    "onReady" : "onReady"
  },
  "height" : "100%"
});
      window.location.href = 'ytIosPlayer://onYouTubeIframeAPIReady';
             });
    function onReady(event) {
        window.location.href = 'ytIosPlayer://onReady?data=' + event.data;
    }
    function onStateChange(event) {
        window.location.href = 'ytIosPlayer://onStateChange?data=' + event.data;
    }

    function onPlaybackQualityChange(event) {
        window.location.href = 'ytIosPlayer://onPlaybackQualityChange?data=' + event.data;
    }
    function onPlayerError(event) {
        window.location.href = 'ytIosPlayer://onError?data=' + event.data;
    }
    </script>
</body>
</html>

它名为&#34; youtubeEmbedHtml.html&#34;将在名为YoutubeIosPlayerView的类中使用,该类将具有将用于嵌入视频的UIWebView。这是代码:

@implementation YoutubeIosPlayerView

@synthesize webView;

- (void)loadVideo:(NSString*)youtubeID
{
    NSError *error = nil;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"youtubeEmbedHtml"
                                                     ofType:@"html"
                                                inDirectory:@"Assets"];
    NSString *embedHTMLTemplate =
        [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    NSString *embedHTML = [NSString stringWithFormat:embedHTMLTemplate, youtubeID];

    //NSLog(@"YTPlayer embed HTML: %@", embedHTML);
    [self.webView loadHTMLString:embedHTML baseURL:[NSURL URLWithString:@"about:blank"]];
    [self.webView setDelegate:self];
    self.webView.allowsInlineMediaPlayback = YES;
    self.webView.mediaPlaybackRequiresUserAction = NO;

}

-(void)playVideo
{
    NSLog(@"Video Played!");
    [self stringFromEvaluatingJavaScript:@"player.playVideo();"];
}

- (void)pauseVideo
{
    NSLog(@"Video Paused!");
    [self stringFromEvaluatingJavaScript:@"player.pauseVideo();"];
}

- (void)stopVideo
{
    NSLog(@"Video Stopped!");
    [self stringFromEvaluatingJavaScript:@"player.stopVideo();"];
}

- (void)clearVideo
{
    [self stringFromEvaluatingJavaScript:@"player.clearVideo();"];
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    NSString* currentURL = self.webView.request.URL.absoluteString;
    NSLog(@"shouldStartLoadWithRequest: URL string = %@\n", currentURL);

    if ([request.URL.scheme isEqual:@"ytIosPlayer"])
    {
        NSString *action = request.URL.host;

        NSString *query = request.URL.query;
        NSString *data;
        if (query)
        {
            data = [query componentsSeparatedByString:@"="][1];
        }

        if ([action isEqual:@"onReady"])
        {
            if ([self.delegate respondsToSelector:@selector(playerViewDidBecomeReady:)])
            {
                [self playVideo]; //Play video if it's successfully loaded
            }
        }
        else if ([action isEqual:@"onStateChange"])
        {
            if ([self.delegate respondsToSelector:@selector(playerView:didChangeToState:)])
            {

                if ([data isEqual:@"0"])
                {
                    [self loadNextVideo]; //Load the next video automatically when the current video stops playing
                }
            }
        }

        return NO;
    }
    else if ([request.URL.scheme isEqual: @"http"] || [request.URL.scheme isEqual:@"https"])
    {
        return [self handleHttpNavigationToUrl:request.URL];
    }
    return YES;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString* currentURL = self.webView.request.URL.absoluteString;
    NSLog(@"webViewDidFinishLoad: URL string = %@\n", currentURL);
    NSString* location = [self.webView stringByEvaluatingJavaScriptFromString:@"window.location"];
    NSLog(@"webViewDidFinishLoad: Location string = %@\n", location);
    NSString* html = [self.webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
    NSLog(@"webViewDidFinishLoad: inner HTML string = %@\n", html);
    NSLog(@"--------------------------------------------------------------------------------");
}

现在,代码实际上工作得很好。但是由于某些情况,当用户点击全屏按钮以及播放器是否正在全屏播放时,我需要能够捕获事件。此外,我需要能够从应用程序以编程方式执行全屏,而不是单击iFrame中的全屏按钮。谁能告诉我怎么做?

提前致谢。

0 个答案:

没有答案