如何在 uiwebview iOS 中处理 YouTube 视频事件(开始、完成等)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5957916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:51:39  来源:igfitidea点击:

How to handle YouTube video events (started, finished, etc) in uiwebview iOS

ioseventsiframeuiwebviewyoutube

提问by Pierpaolo

Currently I'm using the new iframe API to embed a YouTube video inside the uiwebview on the iPad and I've been able to make it auto play without user interactions. In the iframe API it is described how to use the onstatechange event but in my application it doesn't seem to work and unfortunately I can't see any debug in uiwebview.

目前我正在使用新的 iframe API 在 iPad 上的 uiwebview 中嵌入 YouTube 视频,并且我已经能够在没有用户交互的情况下自动播放。在 iframe API 中描述了如何使用 onstatechange 事件,但在我的应用程序中它似乎不起作用,不幸的是我在 uiwebview 中看不到任何调试。

I just want to able able to detect when the video ends, have you got any advice on it? Has anyone got it to work?

我只想能够检测到视频何时结束,您对此有什么建议吗?有没有人让它工作?

回答by CodaFi

Have you tried (from the documentation) to assign an integer to the event of a movie ending?:

您是否尝试过(从文档中)为电影结束事件分配一个整数?:

onStateChange This event fires whenever the player's state changes. The data property of the event object that the API passes to your event listener function will specify an integer that corresponds to the new player state. Possible data values are:

onStateChange 每当玩家的状态发生变化时都会触发此事件。API 传递给事件侦听器函数的事件对象的数据属性将指定一个与新玩家状态对应的整数。可能的数据值为:

-1 (unstarted)
0 (ended)
1 (playing)
2 (paused)
3 (buffering)
5 (video cued).

When the player first loads a video, it will broadcast an unstarted (-1) event. When a video is cued and ready to play, the player will broadcast a video cued (5) event. In your code, you can specify the integer values or you can use one of the following namespaced variables:

当播放器第一次加载视频时,它会广播一个未开始的 (-1) 事件。当视频被提示并准备好播放时,播放器将广播视频提示 (5) 事件。在您的代码中,您可以指定整数值,也可以使用以下命名空间变量之一:

YT.PlayerState.ENDED
YT.PlayerState.PLAYING
YT.PlayerState.PAUSED
YT.PlayerState.BUFFERING
YT.PlayerState.CUED

So, something like:

所以,像这样:

if(event.data==YT.PlayerState.ENDED){ 
//do stuff here
                }

回答by Gth lala

To detect when the video ends then the video has the state of 0 or YT.PlayerState.ENDED

要检测视频何时结束,则视频的状态为 0 或 YT.PlayerState.ENDED

Here's a link to jsfiddle a link!

这是 jsfiddle链接的链接

<!DOCTYPE html>
<html>

  <head>
    <meta charset="UTF-8">
    <title>Page 1</title>
  </head>

  <body>

    <h1>Video page</h1>
    <br>


    <!-- 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 = "https://www.youtube.com/iframe_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 onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'ussCHoQttyQ',
          playerVars: {
            'autoplay': 0,
            'controls': 0,
            'showinfo': 0,
            'rel': 0
          },
          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),
      // -1 unstarted 
      // 0 ended 
      // 1 playing
      // 2 paused
      // 3 buffering
      // 5 video cued
      var done = false;

      function onPlayerStateChange(event) {
        console.log(event);
        if (event.data == YT.PlayerState.ENDED) {
          alert(1);
        }
      }

    </script>




  </body>

</html>

回答by Vaksus

It may be connected with autoplay-policy in your browser (it's disabled by default in mobile browsers).

它可能与浏览器中的 autoplay-policy 相关联(在移动浏览器中默认禁用)。

If your browser is chrome you can start it with additional commandline flag --autoplay-policy=no-user-gesture-required- it worked for me.

如果您的浏览器是 chrome,您可以使用额外的命令行标志启动它--autoplay-policy=no-user-gesture-required- 它对我有用。

回答by Nagaraj

you need to make use of JavaScript for that.

为此,您需要使用 JavaScript。

Refer this link : https://developers.google.com/youtube/js_api_reference

请参阅此链接:https: //developers.google.com/youtube/js_api_reference