Custom HTML5 video player controls with AngularJS

All we need is an easy explanation of the problem, so here it is.

I’m new with AngularJS. I must create customs controls for a video player (HTML5 <video>).
Basically, I would use getElementById('myvideotag'), listen clicks on the video for play/pause.
How I can do this with AngularJS ? Binding the click with ng-click="videoPlayPause()" but then, how I play or pause the video. How I use the classic methods of <video> ?

I guess it’s really simple… I didn’t get all the AngularJS concepts yet !

Thank you 🙂

Oh, the code… in the view:

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

In the right controller:

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

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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 ( 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. ' +
        link: function (scope, element, attrs) {
   = 'whatever url';
            if (vp) vp.dispose();
            vp = videojs('video-' + videoId, {width: 640, height: 480 });

Method 2

How about this:

In your HTML, set ng-click="video($event)" (don’t forget the $event argument), which calls the following function:

$ = function(e) {
    var videoElements = angular.element(e.srcElement);

I believe this is the simplest method.

Documentation for angular.element

Also, this might help you get used to Angular: How do I “think in AngularJS/EmberJS(or other client MVC framework)” if I have a jQuery background?

Method 3

You could also take a look to my project Videogular.

It’s a video player written in AngularJS, so you will have all the benefits of bindings and scope variables. Also you could write your own themes and plugins.

Method 4

I also used videojs

bower install videojs --save

I wanted to use my directive in a ng-repeat and with a scope object, so… here’s my version of it with props to Eduard above. I didn’t seem to have a problem with the video player disposal, but the source tag issue referenced was an actual problem.

I also decided to write this up as an answer, so that I could give an example of how one might want to handle the videojs events.

IMPORTANT! Please note I am using Angular.js with Jinja2 templates, so I had to change my Angular HTML interpolation markers to {[ ]} from {{ }} in case anyone notices that as weird. I’ll include that code too, so it’s not weird for anyone.

Interpolation tweak

app.config(['$interpolateProvider', function($interpolateProvider) {


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-{[ ]}" class="video-js vjs-default-skin img-responsive" controls preload="none"',
          ' ng-src="{[ video.mp4 ]}"',
          ' poster="{[ video.jpg ]}"',
          ' width="240" height="120">',
    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, {},
          this.on("firstplay", function(){
            playLog(vp,, 'firstplay', scope.logToDb);
          this.on("play", function(){
            playLog(vp,, 'play', scope.logToDb);
          this.on("pause", function(){
            playLog(vp,, 'pause', scope.logToDb);
          this.on("seeking", function(){
            playLog(vp,, 'seeking', scope.logToDb);
          this.on("seeked", function(){
            playLog(vp,, 'seeked', scope.logToDb);
          this.on("ended", function(){
            playLog(vp,, 'ended', scope.logToDb);


Directive HTML usage

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

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply