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

发布于 2024-11-06 11:43:40 字数 208 浏览 1 评论 0原文

目前,我正在使用新的 iframe API 将 YouTube 视频嵌入 iPad 上的 uiwebview 中,并且无需用户交互即可使其自动播放。 在 iframe API 中描述了如何使用 onstatechange 事件,但在我的应用程序中它似乎不起作用,不幸的是我在 uiwebview 中看不到任何调试。

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紫轩蝶泪 2024-11-13 11:43:40

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

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

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

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

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

所以,类似:

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

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:

-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:

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
                }
何其悲哀 2024-11-13 11:43:40

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

这是 jsfiddle a 链接

<!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>

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

Here's a link to jsfiddle a link!

<!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>
独木成林 2024-11-13 11:43:40

它可能与您的浏览器中的自动播放策略相关(在移动浏览器中默认禁用)。

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

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

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

少跟Wǒ拽 2024-11-13 11:43:40

为此,您需要使用 JavaScript。

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

you need to make use of JavaScript for that.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文