Skip to content

Commit

Permalink
Added support for advanced usage with iframe API
Browse files Browse the repository at this point in the history
  • Loading branch information
kanzelm3 committed Jul 7, 2015
1 parent df8f408 commit 8293d90
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ video ID.

## Inline Options

There are a number of options that be configured inline with attributes. Here are a few:
There are a number of options that be configured inline with attributes:

| Option | Default | Description |
| -------------------- | ------------------- | ------------------------------------------------------------------------------------------- |
Expand All @@ -59,13 +59,39 @@ There are a number of options that be configured inline with attributes. Here ar
| end | null | Video end time in seconds. If set, video will play until that point and stop (or loop). |
| content-z-index | 99 | If set, will replace the z-index of content within the directive. |
| allow-click-events | false | If set to true, users will be able to click video to pause/play it. |
| player-callback | null | If provided, player callback method will be called with the YouTube player object as the first and only argument. |

**Example:**

```html
<video-bg video-id="video.id" ratio="4/3" loop="false" mute="false" mobile-image="'/img/background-img.png'" start="30" end="120" content-z-index="500" allow-click-events="true"></video-bg>
```

## Advanced Usage

If you need more control over the video (for example, if you need to play/pause the video on a button click), provide a
method with "player" as the only argument to the player-callback attribute.

```html
<video-bg video-id="video.id" player-callback="callback(player)"></video-bg>
```

```javascript
angular.module('myApp').controller(['$scope', function($scope) {
$scope.callback = function(player) {
$scope.pauseVideo = function() {
player.pauseVideo();
};
$scope.playVideo = function() {
player.playVideo();
};
};
});
```

The player object gives you complete access to all of the methods and properties on the player in the
[YouTube IFrame API](https://developers.google.com/youtube/iframe_api_reference#Playback_controls).

## Browser Support

Tested and working in Chrome, Firefox, Safari, Opera and IE 9+.
Expand Down
24 changes: 16 additions & 8 deletions angular-video-bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ angular.module('angularVideoBg', []);
* of the video background.
* @element <video-bg video-id="videoId" ratio="ratio" loop="loop" mute="mute" start="start" content-z-index="contentZIndex" allow-click-events="allowClickEvents"></video-bg>
*/
angular.module('angularVideoBg').directive('videoBg', function($window, $q) {
angular.module('angularVideoBg').directive('videoBg', function($window, $q, $timeout) {
return {
restrict: 'EA',
replace: true,
Expand All @@ -28,28 +28,31 @@ angular.module('angularVideoBg').directive('videoBg', function($window, $q) {
end: '=?',
contentZIndex: '=?',
allowClickEvents: '=?',
mobileImage: '=?'
mobileImage: '=?',
playerCallback: '&?'
},
transclude: true,
template: '<div><div></div><div ng-transclude></div></div>',
link: function(scope, element, attrs, fn) {

scope.ratio = scope.ratio || 16/9;
scope.loop = scope.loop === undefined ? true : scope.loop;
scope.mute = scope.mute === undefined ? true : scope.mute;
scope.mobileImage = scope.mobileImage || '//img.youtube.com/vi/' + scope.videoId + '/maxresdefault.jpg';

var computedStyles,
ytScript = document.querySelector('script[src="//www.youtube.com/iframe_api"]'),
$player = element.children().eq(0),
playerId,
player,
parentDimensions,
playerDimensions;
playerDimensions,
playerCallback = scope.playerCallback;

playerId = 'player' + Array.prototype.slice.call(document.querySelectorAll('div[video-id]')).indexOf(element[0]);
$player.attr('id', playerId);

scope.ratio = scope.ratio || 16/9;
scope.loop = scope.loop === undefined ? true : scope.loop;
scope.mute = scope.mute === undefined ? true : scope.mute;
scope.mobileImage = scope.mobileImage || '//img.youtube.com/vi/' + scope.videoId + '/maxresdefault.jpg';


// Utility methods

function debounce(func, wait) {
Expand Down Expand Up @@ -307,6 +310,11 @@ angular.module('angularVideoBg').directive('videoBg', function($window, $q) {
* This is the method called when the "player" object is ready and can be interfaced with.
*/
function playerReady() {
if (playerCallback) {
$timeout(function() {
playerCallback({ player: player });
});
}
if (scope.mute && !player.isMuted()) {
player.mute();
} else if (player.isMuted()) {
Expand Down
2 changes: 1 addition & 1 deletion angular-video-bg.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "https://github.com/kanzelm3"
},
"name": "angular-video-bg",
"version": "0.2.4",
"version": "0.2.5",
"main": "angular-video-bg.min.js",
"ignore": [
"**/.*",
Expand Down
21 changes: 19 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,26 @@
<script src="angular-video-bg.js"></script>
<!-- Add New Component JS Above -->

<div>
<video-bg video-id="'zk9Acw-vmUs'"></video-bg>
<script type="text/javascript">
angular.module('angularVideoBg').controller('VideoCtrl', ['$scope', '$timeout', function($scope) {

$scope.callback = function(player) {
$scope.pauseVideo = function() {
player.pauseVideo();
};
$scope.playVideo = function() {
player.playVideo();
};
};

}]);
</script>

<div ng-controller="VideoCtrl">
<video-bg video-id="'nfWlot6h_JM'" player-callback="callback(player)"></video-bg>
<p style="padding: 200px 0;">Testing transclude!</p>
<button type="button" ng-click="pauseVideo()" ng-show="pauseVideo">Pause Video</button>
<button type="button" ng-click="playVideo()" ng-show="playVideo">Play Video</button>
</div>
<div>
<video-bg video-id="'cqcldOyEYRE'">
Expand Down

0 comments on commit 8293d90

Please sign in to comment.