Skip to content

Instantly share code, notes, and snippets.

@horozco
Created September 4, 2014 22:07
Show Gist options
  • Select an option

  • Save horozco/671957eefebb65e06c1f to your computer and use it in GitHub Desktop.

Select an option

Save horozco/671957eefebb65e06c1f to your computer and use it in GitHub Desktop.
Custom video controls - Angularjs directive
<div video-controls='true' class="video-container">
<video class="video">
<source ng-src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
</video>
<video-controls></video-controls>
</div>
.video-container {
width: 640px;
height: 365px;
position: relative;
}
.video-controls {
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 5px;
opacity: 0;
-webkit-transition: opacity .3s;
-moz-transition: opacity .3s;
-o-transition: opacity .3s;
-ms-transition: opacity .3s;
transition: opacity .3s;
background-image: linear-gradient(bottom, rgb(3,113,168) 13%, rgb(0,136,204) 100%);
background-image: -o-linear-gradient(bottom, rgb(3,113,168) 13%, rgb(0,136,204) 100%);
background-image: -moz-linear-gradient(bottom, rgb(3,113,168) 13%, rgb(0,136,204) 100%);
background-image: -webkit-linear-gradient(bottom, rgb(3,113,168) 13%, rgb(0,136,204) 100%);
background-image: -ms-linear-gradient(bottom, rgb(3,113,168) 13%, rgb(0,136,204) 100%);
background-image: -webkit-gradient(
linear,
left bottom,
left top,
color-stop(0.13, rgb(3,113,168)),
color-stop(1, rgb(0,136,204))
);
}
.video-container:hover .video-controls {
opacity: .9;
}
button {
background: rgba(0,0,0,.5);
border: 0;
color: #EEE;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
-o-border-radius: 3px;
border-radius: 3px;
}
button:hover {
cursor: pointer;
}
.seek-bar {
width: 360px;
}
.volume-bar {
width: 60px;
}
<div class="video-controls">
<button type="button" class="play-pause" ng-click="playPause()">Play</button>
<input type="range" class="seek-bar" value="0" >
<button type="button" class="mute" ng-click="mute()">Mute</button>
<input type="range" class="volume-bar" min="0" max="1" step="0.1" value="1">
<button type="button" class="full-screen" ng-click="fullScreen()">Full-Screen</button>
</div>
'use strict';
angular.module('nicu')
.directive('videoControls', function() {
return {
restrict: 'E',
scope: true,
templateUrl: 'views/video-controls.html',
controller: function($scope, $element){
var video = $element[0].previousElementSibling;
var playPauseButton = $element.find('button')[0];
var seekBar = $element.find('input')[0];
var mute = $element.find('button')[1];
var volumeBar = $element.find('input')[1];
seekBar.addEventListener("change", function() {
var time = video.duration * (seekBar.value / 100);
video.currentTime = time;
});
video.addEventListener("timeupdate", function() {
var value = (100 / video.duration) * video.currentTime;
seekBar.value = value;
});
seekBar.addEventListener("mousedown", function() {
video.pause();
});
seekBar.addEventListener("mouseup", function() {
video.play();
});
volumeBar.addEventListener("change", function() {
video.volume = volumeBar.value;
});
video.addEventListener('ended', function (){
video.currentTime = 0.1;
});
$scope.playPause = function(){
if (video.paused == true) {
video.play();
playPauseButton.innerHTML = "Pause";
} else {
video.pause();
playPauseButton.innerHTML = "Play";
}
}
$scope.fullScreen = function(){
if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.mozRequestFullScreen) {
video.mozRequestFullScreen(); // Firefox
} else if (video.webkitRequestFullscreen) {
video.webkitRequestFullscreen(); // Chrome and Safari
} else if (video.webkitSupportsFullscreen) {
video.webkitEnterFullscreen();
}
}
$scope.mute = function(){
if (video.muted == false) {
video.muted = true;
mute.innerHTML = "Unmute";
} else {
video.muted = false;
mute.innerHTML = "Mute";
}
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment