Detecting UIWebView finish to play youtube video on iPad

后端 未结 4 957
深忆病人
深忆病人 2020-12-08 06:13

I\'m using UIWebView to play YouTube videos in an iPad.

How can I detect when a YouTube video is finished playing? I see the play icon in the status ba

4条回答
  •  独厮守ぢ
    2020-12-08 06:35

    No, there's no way to directly get web page event from UIWebView. But we can accomplish this by using Javascript.

    • First you use embed Javascript in your custom HTML, to detect video ending play event.
    • Then you try to load a scheme-customized request using JavaScript, and UIWebView could catch the request.

    These links may help:

    1. Calling Objective-C from JavaScript in an iPhone UIWebView

    2. Javascript callback in Objective-C

    3. YouTube iFrame API

    updated with an example:

    in UIWebView's delegate, i put:

    #pragma - mark WebView Delegate
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    
    if ( [[[request URL] scheme] isEqualToString:@"callback"] ) {
    
        NSLog(@"get callback");
    
        return NO;
    }
    
    return YES;
    

    }

    the web page is load when viewDidLoad:

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle       mainBundle] pathForResource:@"youtube" ofType:@"html" ]]]];
    

    and in youtube.html i put:

    
    
    
    

    you can see i add

    if (event.data == YT.PlayerState.ENDED) {
          window.location = "callback:anything";
        };
    

    to YouTube's iFrame API demo, and it catches the player end playing event and try to load a request with scheme "callback", then the UIWebView Delegate could catch it.

    You can use this method to trigger any event using JavaScript;

提交回复
热议问题