【问题标题】:Detect when YouTube embedded video no longer playable?检测 YouTube 嵌入视频何时不再可播放?
【发布时间】:2021-03-24 14:00:51
【问题描述】:

有没有办法检测嵌入的 YouTube 视频是否由于内容限制或删除而无法加载/播放? (而不仅仅是用户的互联网......等)

我在网站上嵌入了一些 YouTube 视频。每隔一段时间,它们就会因为作者删除内容或使其不再公开,或其他任何可能的原因而变得不可播放。

我们的目标是利用某种检测/触发器,然后收到通知或采取一些措施,以确保我的网站上没有大量陈旧/损坏的视频嵌入。

我应该能够使用 API 来轮询每个视频并检查其状态,但如果可能的话,我宁愿采用更被动的方法(或者可能两者兼而有之,但想知道有哪些可用选项)。

【问题讨论】:

    标签: video youtube detection


    【解决方案1】:

    我认为您可以使用 API 中的“onError”事件来了解 youtube 视频是否加载失败。

    function onYouTubeIframeAPIReady() {
      var player;
      player = new YT.Player('player', {
        width: 1280,
        height: 720,
        videoId: 'M7lc1UVf-VE',
        events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange,
          'onError': onPlayerError
        }
      });
    }
    
    function onPlayerError(event) {
      // Handle error ere
    }
    

    或者,如果您使用其他 API 来获取视频详细信息,请查看他们的官方错误文档以找到适合您问题的错误代码。 https://developers.google.com/youtube/v3/docs/errors

    【讨论】:

      【解决方案2】:

      您可以使用嵌入代码 API。您可以只检查视频的时长,如果为零,则视频无法加载。

      https://developers.google.com/youtube/iframe_api_reference

            // with this modification
            function onPlayerReady(event) {
              if (player.getDuration() === 0) console.log("Failed to load video");
            }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-19
        • 2017-02-16
        • 2014-07-04
        • 1970-01-01
        • 1970-01-01
        • 2014-08-02
        • 2011-12-12
        • 2017-11-03
        相关资源
        最近更新 更多