YouTube Javascript API onstateChange 事件问题

发布于 2024-10-14 18:51:46 字数 552 浏览 2 评论 0原文

我的 javascript:

function onYouTubePlayerReady()
{
  playerObj = document.getElementById("player_embed");
  playerObj.addEventListener("onStateChange", test);

  // Make sure the document is loaded before any DOM-manipulation
  $(document).ready(function() {

      // Update player information every 500ms
      setInterval(updatePlayerInfo, 500);

  });
}

function test(newState)
{
    alert(newState);
}

我的视频已正确加载,我可以通过我的 javascript 对其进行精细控制。然而 onStateChange 事件似乎永远不会触发。当我播放/停止/暂停视频等时,它永远不会发出警报。有人有解决方案/类似问题吗?

My javascript:

function onYouTubePlayerReady()
{
  playerObj = document.getElementById("player_embed");
  playerObj.addEventListener("onStateChange", test);

  // Make sure the document is loaded before any DOM-manipulation
  $(document).ready(function() {

      // Update player information every 500ms
      setInterval(updatePlayerInfo, 500);

  });
}

function test(newState)
{
    alert(newState);
}

My videos are loaded correctly and I can control fine them through my javascript. However the onStateChange event never seems to trigger. It never comes up with an alert when I'm playing/stopping/pausing videos etc. Anyone with a solution/similar problem?

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

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

发布评论

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

评论(2

在梵高的星空下 2024-10-21 18:51:46

您必须将函数名称作为字符串传递给 addEventListener

playerObj.addEventListener("onStateChange", "test");

You must pass the function name as a string to addEventListener.

playerObj.addEventListener("onStateChange", "test");
夜血缘 2024-10-21 18:51:46

这种格式适用于 JQuery,但逻辑是相同的,在 onYouTubePlayerReady 中创建一个函数来传递playerId

$(document).ready(function() {              
    onYouTubePlayerReady = function(playerId) {
        eventHandler=function(state){onStateChangeID(state,playerId);}
        playerObj = document.getElementById("player_embed");
        playerObj.addEventListener('onStateChange','eventHandler',false);
    }
    onStateChangeID=function(newState,playerId) {
        console.log("onStateChange: ("+playerId+") " + newState);
    }
});

This format is for JQuery but the logic is same, create a function inside onYouTubePlayerReady to pass the playerId

$(document).ready(function() {              
    onYouTubePlayerReady = function(playerId) {
        eventHandler=function(state){onStateChangeID(state,playerId);}
        playerObj = document.getElementById("player_embed");
        playerObj.addEventListener('onStateChange','eventHandler',false);
    }
    onStateChangeID=function(newState,playerId) {
        console.log("onStateChange: ("+playerId+") " + newState);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文