【问题标题】:Custom HTML5 video player controls with AngularJS使用 AngularJS 自定义 HTML5 视频播放器控件
【发布时间】:2013-07-28 22:30:59
【问题描述】:

我是 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(); // ???
}

【问题讨论】:

  • 问题很清楚,我表达自己错了吗?
  • 现在看起来是个不错的问题。以前,如果没有代码或格式,它看起来就像是一票否决。有人可能会提供帮助,我对 Angular 也不是很有经验。
  • 当然。好,谢谢。在 AngularJS 中,我并不真正了解管理 DOM 的哲学(然而……很难忘记 jQuery 习惯!)
  • @Scofred 查看 Angularjs 的指令。
  • @sza 好的,我如何在这个特定问题中使用指令?你有一个小例子吗?

标签: javascript html video angularjs


【解决方案1】:

为了完全控制,比如行为和外观,我在角度使用videoJS。 我有一个包装video HTML5 元素的ui-video 指令。这是解决与 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 });
        }
    };
});

【讨论】:

    【解决方案2】:

    这个怎么样:

    在您的 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?

    【讨论】:

    • 谢谢!现在我了解了如何操作 DOM。但是,只有在 ng-click 方法中,我在参数和 videoElement[0].pause(); 中添加“$event”时,此代码才有效。
    • 哦,对不起。我稍微编辑了我的答案,现在看起来好吗?
    • 你不应该使用指令来操作 DOM 吗? docs.angularjs.org/guide/directive
    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      我也用过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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-03
        • 1970-01-01
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多