6

我是 AngularJS 的新手。<video>我必须为视频播放器 (HTML5 )创建海关控制。基本上,我会使用getElementById('myvideotag'), listen 点击视频来播放/暂停。我怎么能用 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

4 回答 4

8

For full control, like behaviour and look&feel, I'm using videoJS in angular. I have a ui-video directive that wraps the video HTML5 element. This is necessary to overcome a problem of integration with 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 });
        }
    };
});
于 2013-07-29T07:12:20.353 回答
3

这个怎么样:

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

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

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

angular.element 的文档

此外,这可能会帮助您习惯 Angular:如果我有 jQuery 背景,我如何“在 AngularJS/EmberJS(或其他客户端 MVC 框架)中思考”?

于 2013-07-29T00:40:35.063 回答
3

你也可以看看我的项目 Videogular。

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

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

于 2013-10-25T15:42:13.830 回答
1

我也使用了videojs

bower install videojs --save

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

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

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

插值调整

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>
于 2015-03-06T11:22:49.213 回答