使用AngularJS自定义HTML5视频播放器控件

时间:2013-07-28 22:30:59

标签: javascript html5 video angularjs

我是AngularJS的新手。我必须为视频播放器创建海关控制(HTML5 <video>)。 基本上,我会使用getElementById('myvideotag'),聆听视频点击播放/暂停。 我如何使用AngularJS做到这一点?将点击与ng-click="videoPlayPause()"绑定,但之后,我如何播放或暂停视频。我如何使用<video>的经典方法?

我想这很简单......我还没有得到所有的AngularJS概念!

谢谢:)

哦,代码......在视图中:

<video autoplay="autoplay" preload="auto" ng-click="video()">
    <source src="{{ current.url }}" type="video/mp4" />
</video>

在正确的控制器中:

$scope.video = function() {
    this.pause(); // ???
}

4 个答案:

答案 0 :(得分:8)

对于完全控制,如行为和外观和感觉,我在角度中使用videoJS。 我有一个ui-video指令包装video HTML5元素。这对于克服与AngularJS集成的问题是必要的:

m.directive('uiVideo', function () {
    var vp; // video player object to overcome one of the angularjs issues in #1352 (https://github.com/angular/angular.js/issues/1352). when the videojs player is removed from the dom, the player object is not destroyed and can't be reused.
    var videoId = Math.floor((Math.random() * 1000) + 100); // in random we trust. you can use a hash of the video uri

    return {
        template: '<div class="video">' +
            '<video ng-src="{{ properties.src }}" id="video-' + videoId + '" class="video-js vjs-default-skin" controls preload="auto" >' +
                //'<source  type="video/mp4"> '+     /* seems not yet supported in angular */
                'Your browser does not support the video tag. ' +
            '</video></div>',
        link: function (scope, element, attrs) {
            scope.properties = 'whatever url';
            if (vp) vp.dispose();
            vp = videojs('video-' + videoId, {width: 640, height: 480 });
        }
    };
});

答案 1 :(得分:3)

这个怎么样:

在您的HTML中,设置ng-click="video($event)" (不要忘记$event参数),它会调用以下函数:

$scope.video = function(e) {
    var videoElements = angular.element(e.srcElement);
    videoElements[0].pause();
}

我相信这是最简单的方法。

Documentation for angular.element

此外,这可能有助于您习惯Angular:How do I “think in AngularJS/EmberJS(or other client MVC framework)” if I have a jQuery background?

答案 2 :(得分:3)

您还可以查看我的项目Videogular。

https://github.com/2fdevs/videogular

这是一个用AngularJS编写的视频播放器,因此您将拥有绑定和范围变量的所有好处。你也可以编写自己的主题和插件。

答案 3 :(得分:1)

我还使用了videojs

bower install videojs --save

我想在ng-repeat和范围对象中使用我的指令,所以......这是我的版本,上面有Eduard的道具。我似乎没有处理视频播放器的问题,但source tag issue referenced是一个实际问题。

我还决定把它写成答案,这样我就可以举例说明一个人如何处理videojs事件。

重要!请注意我将Angular.js与Jinja2模板一起使用,因此如果有人注意到,我必须将我的Angular HTML插值标记从{[ ]}更改为{{ }}这很奇怪。我也会包含那些代码,所以对任何人来说都不奇怪。

插值调整

app.config(['$interpolateProvider', function($interpolateProvider) {
  $interpolateProvider.startSymbol('{[');
  $interpolateProvider.endSymbol(']}');
}]);

<强>指令

angular.module('myModule').directive('uiVideo', function () {

  // Function for logging videojs events, possibly to server
  function playLog(player, videoId, action, logToDb) {
    action = action || 'view';
    var time = player.currentTime().toFixed(3);

    if (logToDb) {
      // Implement your own server logging here
    }

    // Paused
    if (player.paused()) {
      console.log('playLog: ', action + " at " + time + " " + videoId);

    // Playing
    } else {
      console.log('playLog: ', action + ", while playing at " + time + " " + videoId);
      if (action === 'play') {
        var wrapFn = function () {
          playLog(player, videoId, action, logToDb);
        };
        setTimeout(wrapFn, 1000);
      }
    }
  }

  return {
    template: [
      '<div class="video">',
        '<video id="video-{[ video.id ]}" class="video-js vjs-default-skin img-responsive" controls preload="none"',
          ' ng-src="{[ video.mp4 ]}"',
          ' poster="{[ video.jpg ]}"',
          ' width="240" height="120">',
        '</video>',
      '</div>'
    ].join(''),
    scope: {
      video: '=video',
      logToDb: '=logToDb'
    },
    link: function (scope, element, attrs) {
      scope.logToDb = scope.logToDb || false;

      var videoEl = element[0].children[0].children[0];
      var vp = videojs(videoEl, {},
        function(){
          this.on("firstplay", function(){
            playLog(vp, scope.video.id, 'firstplay', scope.logToDb);
          });
          this.on("play", function(){
            playLog(vp, scope.video.id, 'play', scope.logToDb);
          });
          this.on("pause", function(){
            playLog(vp, scope.video.id, 'pause', scope.logToDb);
          });
          this.on("seeking", function(){
            playLog(vp, scope.video.id, 'seeking', scope.logToDb);
          });
          this.on("seeked", function(){
            playLog(vp, scope.video.id, 'seeked', scope.logToDb);
          });
          this.on("ended", function(){
            playLog(vp, scope.video.id, 'ended', scope.logToDb);
          });
        }
      );

    }
  };
});

指令HTML使用

  <div ng-repeat="row in videos">
        <ui-video video="row"></ui-video>
  </div>