Angular YouTube Embed
Embed a YouTube player with a simple directive.
$ bower install --save angular-youtube-mb
Can I use it?
Sure! Let me show you.
<!-- Include YT library and this directive -->
// Create your app with 'youtube-embed' dependencyvar myApp = angular;
// Inside your controller...myApp;
<!-- Use 'youtube-video' as an element or attribute. -->
It's that simple. See it in action.
But I only have a URL.
No problem.
$scopeanotherGoodOne = 'https://www.youtube.com/watch?v=18-xvIjH8T4';
Is that it?
Not quite!
Events and Player Controls
youtube.player.ready
youtube.player.ended
youtube.player.playing
youtube.player.paused
youtube.player.buffering
youtube.player.queued
youtube.player.error
Events allow you to keep an eye on the state of things from your controller. For example, if you wanted to a watch a video over and over again forever
myApp;
A full list of player
methods can be found here.
Player Functions
Add player
to embedded youtube player to reference Youtube's video player object to use player functions like playVideo()
, stopVideo()
:
<!-- use 'player' to reference player object. --><!-- perform video playback operations -->PlayStop
Note: playVideo()
, loadVideoById()
won't work in all mobile environments until user initiates playback.
Utilities
These might be handy.
youtubeEmbedUtils.getIdFromURL
youtubeEmbedUtils.getTimeFromURL
Just inject the service into your controller
myApp;
getIdFromURL
is covered with some tests, but let me know if you find any URLs it doesn't support.
Player Parameters
YouTube's embedded player can take a number of optional parameters. You can find a full list here.
For example, you could hide the player's controls and have it start automatically. Add player-vars
to your embedded player.
And define playerVars
in your controller.
$scopeplayerVars = controls: 0 autoplay: 1;
Note: autoplay
won't work on mobile devices.
Player Size
You can set both player-width
and player-height
on the element.
Both values are treated as expressions, which is why the inner single-quotes are need.
Responsive Videos
You'll need to add a few classes to your markup.
I took these classes from Bootstrap, so you might already have them. If not, here they are:
Check out the demo and the code behind it.
Development
First, make sure you have the necessary dependencies installed locally and gulp installed globally
$ npm install$ npm install --global gulp
To build a minfied version to dist/
$ gulp dist
To host the demo on a local server
$ gulp host
To run a couple tests
$ gulp test
And if you want to do all the aforementioned tasks
$ gulp