检测 UIWebView 完成以在 iPad 上播放 YouTube 视频 [英] Detecting UIWebView finish to play youtube video on iPad

查看:24
本文介绍了检测 UIWebView 完成以在 iPad 上播放 YouTube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UIWebView 在 iPad 上播放 YouTube 视频.

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

如何检测 YouTube 视频何时播放完毕?我在状态栏中看到播放图标,我尝试使用 MPMusicPlayerController 通知来检测 playbackStateDidChange,但没有奏效.

How can I detect when a YouTube video is finished playing? I see the play icon in the status bar and I tried to use MPMusicPlayerController notifications to detect playbackStateDidChange, but it didn't work.

任何想法如何检测此事件?再说一次,我说的是 iPad 而不是 iPhone.

Any ideas how to detect this event? Again, I'm talking about iPad not iPhone.

提前致谢.

更新:

如果您使用零解来检测播放结束并且还希望Youtube视频自动启动,请将UIWebView设置为:

If you use zero solution to detect the end of playing and also want the Youtube video to start automatically set the UIWebView to :

self.webView.mediaPlaybackRequiresUserAction = NO ;

我只想澄清一下 YouTube 框架 API:

I just want to clarify about the YouTube frame API:

"重要提示:这是一个实验性功能,这意味着它可能意外改变"(08/05/2012)

"Important: This is an experimental feature, which means that it might change unexpectedly" (08/05/2012)

推荐答案

不,没有办法直接从 UIWebView 获取网页事件.但是我们可以通过使用 Javascript 来实现.

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

  • 首先,您在自定义 HTML 中使用嵌入 Javascript,以检测视频结束播放事件.
  • 然后您尝试使用 JavaScript 加载方案自定义请求,UIWebView 可以捕获该请求.

这些链接可能会有所帮助:

These links may help:

  1. 在 iPhone UIWebView 中从 JavaScript 调用 Objective-C

Objective-C 中的 JavaScript 回调

YouTube iFrame API

更新了一个例子:

在 UIWebView 的委托中,我放了:

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;

}

网页在viewDidLoad时加载:

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

在 youtube.html 中我输入:

and in youtube.html i put:

<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>

<script>

  // 2. This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');
  tag.src = "http://www.youtube.com/player_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // 3. This function creates an <iframe> (and YouTube player)
  //    after the API code downloads.
  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'u1zgFlCw8Aw',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }

  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
    event.target.playVideo();
  }

  // 5. The API calls this function when the player's state changes.
  //    The function indicates that when playing a video (state=1),
  //    the player should play for six seconds and then stop.
  var done = false;
  function onPlayerStateChange(event) {

    if (event.data == YT.PlayerState.PLAYING && !done) {
      setTimeout(stopVideo, 6000);
      done = true;
    }
    if (event.data == YT.PlayerState.ENDED) {
      window.location = "callback:anything"; //here's the key
    };
  }
  function stopVideo() {
    player.stopVideo();
  }
</script>
</body>
</html>

你可以看到我添加

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

到 YouTube 的 iFrame API 演示,它捕获播放器结束播放事件并尝试加载带有回调"方案的请求,然后 UIWebView 委托可以捕获它.

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.

您可以使用此方法来触发任何使用 JavaScript 的事件;

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

这篇关于检测 UIWebView 完成以在 iPad 上播放 YouTube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆