在Angular指令中,Froogaloop Vimeo'完成'事件

时间:2014-06-27 11:42:47

标签: angularjs angularjs-directive vimeo froogaloop

我在Angular指令中触发Vimeo“Finish”事件时遇到问题。 正在加载Vimeo视频,播放和暂停功能正常工作。

我的HTML代码

<vimeo control-boolean="isPlaying" vid="{{videoslist[id].code}}" pid="1"></vimeo>
<button ng-click="prevVid()">Prev</button>
<button ng-if="isPlaying" ng-click="status()">Pause</button>
<button ng-if="!isPlaying" ng-click="status()">Play</button>
<button ng-click="nextVid()">Next</button>
<div class="videoInfo">
  <h2>{{videoslist[id].title}}</h2>
  <p>{{videoslist[id].discription}}</p>
</div>

我的AngularJS指令

directive('vimeo', function($sce) {
  return {
      restrict: 'EA',
      replace: true,
      scope: {
        //Assumes that true means the video is playing
        controlBoolean: '='
      },
      template: '<iframe id="video"></iframe>',
      link: function postLink(scope, element, attrs) {
        attrs.$observe('vid', function(value) {
          var url = "http://player.vimeo.com/video/" + attrs.vid + "?title=0&byline=0&portrait=0&api=1&autoplay=true";
          element.attr('src', url);
          var iframe = element[0],
          player = $f(iframe);
          player.addEvent("ready", function() {        
            player.addEvent('finish', onFinish);
          });
          function onFinish() {
            console.log("vimeo finish")
          }
        scope.$watch('controlBoolean', function(){
          if(scope.controlBoolean){
            player.api('play');
          }
          else{
            player.api('pause');
          }
        });
        });

      }
    };
  });

在上面的指令中,这一行应该触发事件,但它不是:

player.addEvent("ready", function() {        
  player.addEvent('finish', onFinish);
});
function onFinish(id) {
  console.log("vimeo finish")
}

我用它来帮助我指令: http://embed.plnkr.co/GKWNk3LhX0MR3lhpfqyA/preview

和Vimeo的网站,阅读“如何触发事件”: http://developer.vimeo.com/player/js-api

有人看到我做错了吗?

1 个答案:

答案 0 :(得分:0)

您应该像这样添加完成就绪事件的代码:

scope.$watch('controlBoolean', function(){
           if(scope.controlBoolean){
              player.api('play');
              player.addEvent("ready", function() {  
                 console.log("vimeo ready");
                 player.addEvent('finish', onFinish);
              });
         } else {
           player.api('pause');
         }
       });

我修改了这个plunker,它对我有用。