• Stars
    star
    509
  • Rank 86,381 (Top 2 %)
  • Language
    HTML
  • License
    MIT License
  • Created over 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📺 Embed a YouTube player with a simple directive

Angular YouTube Embed

Embed a YouTube player with a simple directive.

$ npm install --save angular-youtube-embed

or

$ bower install --save angular-youtube-mb

Can I use it?

Sure! Let me show you.

<!-- Include YT library and this directive -->
<script src="https://www.youtube.com/iframe_api"></script>
<script src="angular-youtube-embed.js"></script>
// Create your app with 'youtube-embed' dependency
var myApp = angular.module('myApp', ['youtube-embed']);
// Inside your controller...
myApp.controller('MyCtrl', function ($scope) {
  // have a video id
  $scope.theBestVideo = 'sMKoNBRZM1M';
});
<!-- Use 'youtube-video' as an element or attribute. -->
<youtube-video video-id="theBestVideo"></youtube-video>

It's that simple. See it in action.

But I only have a URL.

No problem.

$scope.anotherGoodOne = 'https://www.youtube.com/watch?v=18-xvIjH8T4';
<youtube-video video-url="anotherGoodOne"></youtube-video>

What about browserify?

If you are using browserify or webpack, make sure you've installed this module:

$ npm install --save angular-youtube-embed

and use it in your code like this:

require('angular-youtube-embed');

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.controller('MyCtrl', function ($scope) {
  $scope.looper = 'VvTvtIeXJ1I';

  $scope.$on('youtube.player.ended', function ($event, player) {
    // play it again
    player.playVideo();
  });
});

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. -->
<youtube-video video-id="'sMKoNBRZM1M'" player="bestPlayer"></youtube-video>
<!-- perform video playback operations -->
<button ng-click="bestPlayer.playVideo()">Play</button>
<button ng-click="bestPlayer.stopVideo()">Stop</button>

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.controller('MyCtrl', function ($scope, youtubeEmbedUtils) {
  // 'VvTvtIeXJ1I'
  console.log(youtubeEmbedUtils.getIdFromURL('https://www.youtube.com/watch?v=VvTvtIeXJ1I'));
});

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.

<youtube-video video-id="theBestVideo" player-vars="playerVars"></youtube-video>

And define playerVars in your controller.

$scope.playerVars = {
    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.

<youtube-video video-id="theBestVideo" player-width="'100%'" player-height="'300px'"></youtube-video>

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.

<div class="embed-responsive embed-responsive-16by9">
  <youtube-video class="embed-responsive-item" video-id="theBestVideo"></youtube-video>
</div>

I took these classes from Bootstrap, so you might already have them. If not, here they are:

.embed-responsive {
  position: relative;
  display: block;
  height: 0;
  padding: 0;
  overflow: hidden;
}

.embed-responsive.embed-responsive-16by9 {
  padding-bottom: 56.25%;
}

.embed-responsive-item {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

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
$ bower 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

More Repositories

1

Lax

IRC client built with Electron & React
JavaScript
137
star
2

react-tetris

🎮 by popular demand
TypeScript
136
star
3

angular-adaptive-backgrounds

🌄 Surround a picture with its dominant color using a simple directive
CoffeeScript
106
star
4

wren-json

Handling JSON for Wren
51
star
5

butter.js

🍴 pixel sorting with canvas
JavaScript
42
star
6

lox.js

JavaScript
19
star
7

angular-drag-to-reorder

✊ Drag items in an ng-repeat to reorder them
CoffeeScript
19
star
8

wren-pure

Utility library for Wren
11
star
9

wrenpm

A package manager for Wren
JavaScript
11
star
10

elm-dr-mario

👨‍⚕️💊🦠
Elm
10
star
11

elm-to-dot

Crawl an Elm project and produce a dependency graph in DOT
Elm
10
star
12

elm-minesweeper

💣
Elm
10
star
13

thelist

📃 play sounds with soundcloud
JavaScript
10
star
14

lisp.js

a lisp interpreter in javascript
JavaScript
9
star
15

bf.go

😵 brainfuck in golang
Go
9
star
16

elm-slime-volleyball

🔵🔴 try to beat the blue slime
Elm
9
star
17

presidential-election-data

U.S. presidential election data in JSON
JavaScript
8
star
18

scales

🎹 play musical scales ON THE INTERNET
JavaScript
8
star
19

elm-rocket-league

⚽️🚙💨
Elm
7
star
20

boof

it's really really good
TypeScript
7
star
21

Snapchat.js

🎨 drop file in, draw like in snapchat
JavaScript
7
star
22

twitter2snapchat-server

API for converting Twitter handles to Snapchat handles
JavaScript
6
star
23

jim

it's a bot that likes basketball
CoffeeScript
6
star
24

pablo

💿 make ur own album cover
JavaScript
5
star
25

first-listen-dl

cop that 🔥🔥🔥
JavaScript
5
star
26

advent-of-code

some solutions
JavaScript
5
star
27

launchpad-marquee

Use your Novation Launchpad as a scrolling marquee
JavaScript
5
star
28

redditelly

watch vids
CoffeeScript
4
star
29

elm-dot-lang

DOT Language
Elm
4
star
30

jupiter

an album cover
CoffeeScript
4
star
31

overturn-presidential-election

JavaScript
4
star
32

weave.js

commonjs module bundler that's v fast
JavaScript
4
star
33

life

JavaScript
3
star
34

say-time

JavaScript
3
star
35

dots

a game about connecting
JavaScript
3
star
36

all-the-free-drum-kits

download lots of noises
JavaScript
3
star
37

mit-press-open-access

catalog of direct links to Open Access books
JavaScript
3
star
38

5hundo

🐴 @gilesvangruisen owned it so
JavaScript
3
star
39

voronoi

JavaScript
3
star
40

allelm

download elm.json file for every published Elm package and generate a force-directed graph layout
JavaScript
2
star
41

viz

audio visualization with canvas
CSS
2
star
42

launchpad-s-reader

read input from Launchpad S MIDI controller
JavaScript
2
star
43

tunes

super broken music player built on node-webkit
CSS
2
star
44

rosalind

some solutions
Haskell
2
star
45

rl-easy21

TypeScript
2
star
46

node-quine

neat
JavaScript
2
star
47

elm-reddit

https://brandly.github.io/elm-reddit/#r/javascript
Elm
2
star
48

first-listen-api

Query data from NPR First Listen
JavaScript
2
star
49

lisp-repl

in your browser since 2015
JavaScript
2
star
50

jstro-synesthesia

turn letters into blocks of color, according to jstro's own colors
JavaScript
2
star
51

harvard-food-trucks-cli

check on Chicken & Rice Guys
JavaScript
2
star
52

hubot-nba

A hubot script for NBA stats
CoffeeScript
2
star
53

als2xml

Convert .als files into .xml files
JavaScript
2
star
54

Sandwich

a terrible app for browsing reddit
Swift
2
star
55

allcrates

Generate a dependency graph for every Rust crate to be viewed in pm
JavaScript
2
star
56

Sudoku

2
star
57

schiff

simple, real time chat with Angular, Node, and Socket.io
JavaScript
2
star
58

grill

peer-to-peer video chat
JavaScript
2
star
59

drunk-tweet

JavaScript
1
star
60

blackboard-course-cleaner

JavaScript
1
star
61

hello-github-actions

Dockerfile
1
star
62

js1k-2013

JavaScript
1
star
63

sample_app

rails tutorial
Ruby
1
star
64

flickr-lightbox

JavaScript
1
star
65

elm-arch

matt learns elm
Elm
1
star
66

slack-noteshrink

A slackbot that shrinks your notes
Python
1
star
67

rust-raytracer

Rust
1
star
68

go-wiki

Go
1
star
69

charley-proj

Jupyter Notebook
1
star
70

lcd

looks kind of cool
JavaScript
1
star
71

2048

JavaScript
1
star
72

lisp-cli

JavaScript
1
star
73

moire

cool lines
CoffeeScript
1
star
74

blackboard-download-video

HTML
1
star
75

learn-server-rendering

JavaScript
1
star
76

sublime-indent-depth

change how many spaces are used to indent a file
Python
1
star
77

react-flame-graph

TypeScript
1
star
78

xor-nn

a small, feedforward neural network that quickly learns how to XOR
JavaScript
1
star
79

Curriculum

Create online courses
JavaScript
1
star
80

js-sudoku

solve sudoku real fast
JavaScript
1
star
81

egghead-react

just some code from a tutorial
JavaScript
1
star
82

bash-hunnid

Shell
1
star
83

rust-lox

Rust
1
star
84

ss15-queso

The official competition repo for team queso
JavaScript
1
star
85

elm-fill-in-the-blanks

an elm project that doesn't do anything
Elm
1
star
86

learn-three

JavaScript
1
star
87

yahtzee-textract

🗒 read a yahtzee score card and do the math
JavaScript
1
star
88

shootings

List of school shootings in the United States
Jupyter Notebook
1
star
89

uniballer

schools that produced a single NBA player
HTML
1
star
90

flock

JavaScript
1
star
91

elm-flame-graph

🔥 render a flame graph in your Elm app
Elm
1
star