Bài viết này thực hiện (hoặc lụm bài về đăng câu like từ các trang khác) bởi Việt Lâm Coder một YOUTUBER có tâm và đẹp trai siêu cấp vô địch zũ trụ.
Mục lục
YouTube Player API wrapper like HTML5 video API.
Usage
Basic
See examples/basic.html
.
1 |
<<span class="pl-ent">div</span> <span class="pl-e">id</span>=<span class="pl-s"><span class="pl-pds">"</span>my-youtube-player<span class="pl-pds">"</span></span> <span class="pl-e">data-youtube-videoid</span>=<span class="pl-s"><span class="pl-pds">"</span>KFstP0C9sVk<span class="pl-pds">"</span></span>></<span class="pl-ent">div</span>> |
1 2 3 |
<span class="pl-k">var</span> elPlayer <span class="pl-k">=</span> <span class="pl-c1">document</span>.<span class="pl-c1">getElementById</span>(<span class="pl-s"><span class="pl-pds">'</span>my-youtube-player<span class="pl-pds">'</span></span>); <span class="pl-en">youtube</span>({ el<span class="pl-k">:</span>elPlayer }); |
Player
See examples/player.html
.
1 2 3 4 5 |
<<span class="pl-ent">div</span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>js-player<span class="pl-pds">"</span></span>></<span class="pl-ent">div</span>> <<span class="pl-ent">div</span>> <<span class="pl-ent">button</span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>js-play<span class="pl-pds">"</span></span> <span class="pl-e">disabled</span>>Play</<span class="pl-ent">button</span>> <<span class="pl-ent">button</span> <span class="pl-e">class</span>=<span class="pl-s"><span class="pl-pds">"</span>js-pause<span class="pl-pds">"</span></span> <span class="pl-e">disabled</span>>Pause</<span class="pl-ent">button</span>> </<span class="pl-ent">div</span>> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
(<span class="pl-k">function</span>() { <span class="pl-k">var</span> elPlayer <span class="pl-k">=</span> <span class="pl-c1">document</span>.<span class="pl-c1">querySelector</span>(<span class="pl-s"><span class="pl-pds">'</span>.js-player<span class="pl-pds">'</span></span>); <span class="pl-k">var</span> elPlay <span class="pl-k">=</span> <span class="pl-c1">document</span>.<span class="pl-c1">querySelector</span>(<span class="pl-s"><span class="pl-pds">'</span>.js-play<span class="pl-pds">'</span></span>); <span class="pl-k">var</span> elPause <span class="pl-k">=</span> <span class="pl-c1">document</span>.<span class="pl-c1">querySelector</span>(<span class="pl-s"><span class="pl-pds">'</span>.js-pause<span class="pl-pds">'</span></span>); <span class="pl-k">var</span> videoId <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>KFstP0C9sVk<span class="pl-pds">'</span></span>; <span class="pl-k">var</span> player <span class="pl-k">=</span> <span class="pl-en">youtube</span>({ el<span class="pl-k">:</span>elPlayer, id<span class="pl-k">:</span>videoId }) .<span class="pl-en">on</span>(<span class="pl-s"><span class="pl-pds">'</span>ready<span class="pl-pds">'</span></span>, <span class="pl-k">function</span>(<span class="pl-c1">event</span>) { <span class="pl-smi">player</span>.<span class="pl-c1">play</span>(); }) .<span class="pl-en">on</span>(<span class="pl-s"><span class="pl-pds">'</span>play<span class="pl-pds">'</span></span>, <span class="pl-k">function</span>(<span class="pl-c1">event</span>) { <span class="pl-smi">elPlay</span>.<span class="pl-c1">disabled</span> <span class="pl-k">=</span> <span class="pl-c1">true</span>; <span class="pl-smi">elPause</span>.<span class="pl-c1">disabled</span> <span class="pl-k">=</span> <span class="pl-c1">false</span>; }) .<span class="pl-en">on</span>(<span class="pl-s"><span class="pl-pds">'</span>pause<span class="pl-pds">'</span></span>, <span class="pl-k">function</span>(<span class="pl-c1">event</span>) { <span class="pl-smi">elPlay</span>.<span class="pl-c1">disabled</span> <span class="pl-k">=</span> <span class="pl-c1">false</span>; <span class="pl-smi">elPause</span>.<span class="pl-c1">disabled</span> <span class="pl-k">=</span> <span class="pl-c1">true</span>; }) .<span class="pl-en">on</span>(<span class="pl-s"><span class="pl-pds">'</span>ended<span class="pl-pds">'</span></span>, <span class="pl-k">function</span>(<span class="pl-c1">event</span>) { <span class="pl-smi">player</span>.<span class="pl-c1">play</span>(); }); <span class="pl-smi">elPlay</span>.<span class="pl-c1">addEventListener</span>(<span class="pl-s"><span class="pl-pds">'</span>click<span class="pl-pds">'</span></span>, <span class="pl-k">function</span>(<span class="pl-c1">event</span>) { <span class="pl-smi">player</span>.<span class="pl-c1">play</span>(); }); <span class="pl-smi">elPause</span>.<span class="pl-c1">addEventListener</span>(<span class="pl-s"><span class="pl-pds">'</span>click<span class="pl-pds">'</span></span>, <span class="pl-k">function</span>(<span class="pl-c1">event</span>) { <span class="pl-smi">player</span>.<span class="pl-c1">pause</span>(); }); })(); |
References
Interface
youtube(options)
options.el
… {Element} element that will be replaced with YouTube Player.options.id
… {String} video ID like"KFstP0C9sVk"
.
Additionally, every Youtube Player Parameters can be passed as options. Check the Youtube documentation for the full list
Those options accepts the values stated in the youtube documentation, but can also accept booleans in place of 1 (true)
and 0 (false)
. Some player parameters exemples :
options.autoplay
… {Boolean | 0 or 1} start playing automatically if true or 0. Optional. Default is0
.options.controls
… {Boolean | 0 or 1} show controll UIs if true. Optional. Default is1
.options.disabledkb
… {Boolean | 0 or 1} enable or disabled keyboard control. Optional. Default is0
.options.start
… {Number} specify a start time for the video. Optional. Default isundefined
.options.end
… {Number} specify a start time for the video. Optional. Default isundefined
.options.showInfo
… {Boolean | 0 or 1} show video info (title, author…). Optional. Default is1
.- returns … {Player}
1 2 3 4 5 6 |
<span class="pl-k">var</span> el <span class="pl-k">=</span> <span class="pl-c1">document</span>.<span class="pl-c1">querySelector</span>(<span class="pl-s"><span class="pl-pds">'</span>#the-player<span class="pl-pds">'</span></span>); <span class="pl-k">var</span> videoId <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>KFstP0C9sVk<span class="pl-pds">'</span></span>; <span class="pl-k">var</span> player <span class="pl-k">=</span> <span class="pl-en">youtube</span>({ el<span class="pl-k">:</span>el, id<span class="pl-k">:</span>videoId }); <span class="pl-smi">player</span>.<span class="pl-c1">addEventListener</span>(<span class="pl-s"><span class="pl-pds">'</span>ready<span class="pl-pds">'</span></span>, <span class="pl-k">function</span>(<span class="pl-c1">event</span>) { <span class="pl-c1">this</span>.<span class="pl-c1">play</span>(); }); |
Methods
play()
Start playing a video.
pause()
Stop playing a video.
addEventListener(type, listener)
Set an event listener.
type
… {string} event name.listener
… {Function} event listener.
removeEventListener(type, listener)
Remove an event listener.
type
… {string} event name.listener
… {Function} event listener.
on(type, listener)
Shortcut for addEventListener()
. This method is chainable.
type
… {string} event name.listener
… {Function} event listener.- returns … {Player}
off(type, listener)
Shortcut for removeEventListener()
. This method is chainable.
type
… {string} event name.listener
… {Function} event listener.- returns … {Player}
destroy()
Remove player.
The target element is restored, event listeners are detached, player properties are cleared.
Properties
player
Original YouTube Player object.
- type … YT.Player
duration
How long time in seconds of the currently playing video.
- type … Number
currentSrc
YouTube.com URL for the currently loaded/playing video.
- type … String
paused
True if playback is paused; false otherwise.
- type … Boolean
ended
True if playback has reached the end
- type … Boolean
Functional Properties
Properties are implemented as getter and setter functions.
1 2 |
<span class="pl-k">var</span> currentTime <span class="pl-k">=</span> <span class="pl-smi">player</span>.<span class="pl-smi">currentTime</span>; <span class="pl-c">// getter</span> <span class="pl-smi">player</span>.<span class="pl-smi">currentTime</span> <span class="pl-k">=</span> <span class="pl-c1">123</span>; <span class="pl-c">// setter</span> |
If you use youtube.compat.js
, these getter and setter are changed to just functions.
1 2 |
<span class="pl-k">var</span> currentTime <span class="pl-k">=</span> <span class="pl-smi">player</span>.<span class="pl-en">currentTime</span>(); <span class="pl-c">// getter</span> <span class="pl-smi">player</span>.<span class="pl-en">currentTime</span>(<span class="pl-c1">123</span>); <span class="pl-c">// setter</span> |
currentTime
- type … Number
Returns the current playback position, in seconds, as a position between zero time and the current duration.
Can be set, to seek to the given time.
Updated with progress.
volume
- type … Number
Returns the current playback volume multiplier, as a number in the range 0.0 to 1.0, where 0.0 is the quietest and 1.0 the loudest.
Can be set, to change the volume multiplier.
muted
- type … Boolean
Returns true if all audio is muted (regardless of other attributes either on the controller or on any media elements slaved to this controller), and false otherwise.
Can be set, to change whether the audio is muted or not.
playbackRate
- type … Number
Returns the current rate of playback.
Can be set, to change the rate of playback.
This value is NOT available in compat mode. (Always 1
)
src
YouTube.com URL for the loaded/playing video.
Can be set, to change the video.
- type … String
Events
Type | When |
---|---|
ready |
The player is ready to use. |
error |
Any error is occurred. |
emptied |
The video is refreshed. |
canplay |
The video is ready to play. |
canplaythrough |
The video is ready to play. |
playing |
Started playing. |
ended |
Finished playing. |
durationchange |
Duration is changed. |
timeupdate |
Current playback time is changed. |
play |
Started playing. |
pause |
Stopped playing. |
ratechange |
Playback rate is changed. |
volumechange |
Volume is changed, muted or unmuted. |
Compatibility With HTML5 Video API
- ✔ = Compatible (or almost)
- △ = Similar (but not compatible)
- ✘ = Not supported
- ☆ = Original feature
- ? = Ah, let me see…
Function | Status | Description |
---|---|---|
addEventListener() |
✔ | |
destroy() |
☆ | |
off() |
☆ | Simple shortcut for removeEventListener() |
on() |
☆ | Simple shortcut for addEventListener() |
pause() |
✔ | |
play() |
✔ | |
removeEventListener() |
✔ | |
currentSrc |
✔ | |
currentTime |
✔ | |
duration |
✔ | |
ended |
✔ | |
muted |
✔ | |
paused |
✔ | |
playbackRate |
△ | Only 0.25 , 0.5 , 1 , 1.5 , or 2 (Check YouTube API) |
player |
☆ | |
src |
✔ | |
volume |
✔ | |
canplay event |
△ | Same as YouTube onReady event |
canplaythrough event |
△ | Same as YouTube onReady event |
durationchange event |
✔ | |
emptied event |
? | |
ended event |
✔ | |
error event |
✔ | Same as YouTube onError event |
muted event |
✔ | |
pause event |
✔ | |
play event |
✔ | |
playing event |
? | |
progress event |
✘ | |
ratechange event |
✔ | |
ready event |
☆ | |
seeked event |
✘ | |
seeking event |
✘ | |
src event |
✔ | |
timeupdate event |
✔ | |
volumechange event |
✔ |
Browsers
- IE 9+
- Modern PC Browsers
Support IE 7, 8
Use with youtube.compat.js
. See examples/full-player.compat.html
.
1 2 3 4 5 |
<<span class="pl-ent">script</span> <span class="pl-e">src</span>=<span class="pl-s"><span class="pl-pds">"</span>html5-youtube.js<span class="pl-pds">"</span></span>></<span class="pl-ent">script</span>> <<span class="pl-ent">script</span> <span class="pl-e">src</span>=<span class="pl-s"><span class="pl-pds">"</span>html5-youtube.compat.js<span class="pl-pds">"</span></span>></<span class="pl-ent">script</span>> <<span class="pl-ent">script</span>> <span class="pl-s1"><span class="pl-k">var</span> player <span class="pl-k">=</span> <span class="pl-en">youtube</span>({ <span class="pl-k">...</span> });</span> </<span class="pl-ent">script</span>> |
Compatible mode forces some rules.
options.autoplay
is not available in initializing.options.controls
is not available in initializing.- Functional properties have another interfaces. See its section.
Download
Link : html5-youtube.js-master
Bài viết này thực hiện (hoặc lụm bài về đăng câu like từ các trang khác) bởi Việt Lâm Coder một YOUTUBER có tâm và đẹp trai siêu cấp vô địch zũ trụ.