How do I attach a jQuery event handler to a YouTube movie?

前端 未结 4 1702
走了就别回头了
走了就别回头了 2020-12-30 17:16

[EDIT: Sorry to those who already answered -- in my sleep-deprived state, I forgot that this particular situation is a YouTube movie, not the JW FLV player. I can see th

相关标签:
4条回答
  • 2020-12-30 17:48

    What you're looking for is the flash ExternalInterface class, which is used for communication from flash to javascript and from javascript to flash.

    0 讨论(0)
  • 2020-12-30 17:57

    Flash movies are pretty much black boxes as far as javascript is concerned. If the SWF you're using wasn't written to interact with javascript then you're probably out of luck.

    You'll either need to figure out what javascript methods the movie you're using exposes (hopefully it has documentation), find another one that does provide javascript interaction, or write your own SWF to handle it.

    0 讨论(0)
  • 2020-12-30 18:00

    The YouTube player API is pretty straight-forward. You just have to listen to the onStateChange event and control the cycle plugin depending on the state:

    Here's a working demo: http://jsbin.com/izolo (Editable via http://jsbin.com/izolo/edit)

    And the pertinent code:

    function handlePlayerStateChange (state) {
      switch (state) {
        case 1:
        case 3:
          // Video has begun playing/buffering
          videoContainer.cycle('pause');
          break;
        case 2:
        case 0:
          // Video has been paused/ended
          videoContainer.cycle('resume');
          break;
      }
    }
    
    function onYouTubePlayerReady(id){
      var player = $('#' + id)[0];
      if (player.addEventListener) {
        player.addEventListener('onStateChange', 'handlePlayerStateChange');
      }
      else {
        player.attachEvent('onStateChange', 'handlePlayerStateChange');
      }
    }
    
    0 讨论(0)
  • 2020-12-30 18:08

    If you're embedding the player using swfobject you're going to want to use swfobject.getObjectById to get a reference to the movie. Read the docs to see why you need to do this.

    Also you'll need to set {wmode:"transparent"} for the player in order for it to bubble up click events to JavaScript.

    0 讨论(0)
提交回复
热议问题